Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9174823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:48:55+00:00 2026-06-17T16:48:55+00:00

I have a class with some members whose value has a finite range. For

  • 0

I have a class with some members whose value has a finite range. For example

class Sphere
{
public:
    void setRadius(double radius)
    {
        m_radius = radius;
    }

private:
    double m_radius; // must >= 0.
};

I also have a dialog to input the radius. I can check the radius validation in the setRadius() method, or check in the dialog. Which way is better? This seems quite a common problem. What is the conventional way or best way? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:48:56+00:00Added an answer on June 17, 2026 at 4:48 pm

    In OOP, you typically have the validation occur in the function and have the function throw an exception if the value is not within your accepted range, in your case.

    However, if you’re doing this for a class and aren’t expected to know about throwing/catching exceptions yet, you can do one of two things:

    1. Have your function return an int, bool, etc. If the argument is within range, then your function returns some value indicating thus. Else, the function returns some value indicating the argument was not within the range.

    Something like this:

    bool setRadius(double radius)
    {
        if(radius >= 0)
        {
            m_radius = radius;
            return true;
        }
        else return false;
    }
    

    Notice that you don’t initiate/change the value stored in m_radius if the argument of “radius” is not within your range. There would be no point in changing the value of m_radius to an unacceptable value — it’s a waste of time.

    1. Check it in main() (“in the dialog”?)

    If you want to know more about exceptions, you can refer to this page for a greater understanding of what an exception is: http://www.cplusplus.com/doc/tutorial/exceptions/

    So, i++ and to conclude, the convention is to check it in the method and have the method throw an exception if the value is out of the accepted range. You may not have to if you’re in a class and haven’t covered exceptions yet though. Best of luck to you! 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class Trigger with some members of type SimpleAction : public SimpleAction
I have a class in system-C with some data members as such: long double
I have a very simple class with some char* members. Is strcat the best
If I have a class with some value member that I want to store
Suppose I have some class which has a property actor_ of type Actor .
So I have a class is something like this public class Order { //some
the basic idea is that you have some class that has a reference type
class SomeClass { //some members MemberClass one_of_the_mem_; } I have a function foo( SomeClass
I have a std::map<string, double> whose members look something like: X = [{N, 200},
I'm using protobuf-net v2 (r480) and I have a MetaType class where some members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.