dependencies about class members and functions

Hi, what if a class function depends on the value of a class member? So for example I need to call function() of the class object foo:
1
2
myclass foo;
foo.function()


but function() needs to have its member X to be defined before it is called, otherwise if fails;

1
2
3
4
5
6
7
8
9
10
myclass foo;
foo.X = 1;
foo.function()


myclass::function()
{
   if ( X != 1 )
   return;
}


How do you cope with this?
Constructors of a class shall initialize its members. Or you can use exceptions for member functions.

For example,

1
2
3
4
5
6
class myClass
{
public:
// other declarations
   void f() throw ( std::out_of_range );
};
Last edited on
That is true but in my case the final value f() has to work on cannot be known in advance (so constructor does not help) but it needs to be defined by the caller object before it calls f(). Does it make sense?
needs to be defined by the caller object before it calls f()


What do you mean, exactly? Where is the value of X going to come from?
for instance I have

1
2
3
4
5
6
7
8
9
myclass foo;
foo.X = 1;
foo.function()


myclass::function()
{
   // do some stuff which require X to be set to _any_ number but 0, otherwise it does not work
}


and the member X is defined like above, before calling function()

An example from a real scenario would be.. I need to set a valid IP address as value of the member of the class, then I can call the function which uses that value to ping it... this is what I mean as dependencies between members and functions
Last edited on
If it's relying on a variable being initialised, can't you just put a check in the function body? Maybe give it a return type, indicating that it hasn't been initialised. Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Class definition
class MyClass
{
private:
   int x;
public:
   MyClass();
   int MyFunction();
   void SetX(int val);

// Constructor
MyClass::MyClass() : x(-1)
{
}

// Functions
int MyClass::MyFunction()
{
   if (x == -1)
   {
      return -1;
   }
   else
   {
      // Do stuff here
      return 0;
   }
}

void MyClass::SetX(int val)
{
   x = val;
}

// Function call in code
int ret = -1;
int input = -1;
MyClass foo;

ret = foo.MyFunction();
while (ret == -1)
{
   cout << "MyFunction returned failure. Please enter x:";
   cin >> input;
   foo.SetX(input);
   ret = foo.MyFunction();
}
   cout << "MyFunction returned success";


I would define some constant names or some #defines for the return values (something like SUCCESS, FAIL, etc).

To be honest, it's a pretty crude solution to error-checking and there's far more elegant ways of doing it. However, it should do the trick. You'll also probably want some sort of validation on the user input.
Last edited on
I need to set a valid IP address as value of the member
If this is the example then I would say have the constructor set the IP address to local host. Then in your program, whoever is responsible for calling the "ping" function needs to set a new IP address, obviously, if they don't want to ping the local host?

I mean, what you are describing is common in programming. Objects get initialized with default values, later in the code they are set to other non default values, some other object calls member functions of that same object. I mean even in your description you answer your own question.

Hi, what if a class function depends on the value of a class member? So for example I need to call function() of the class object foo:
but function() needs to have its member X to be defined before it is called, otherwise if fails;
How do you cope with this?


The same way you would with any other logic, it is a condition that X needs to be set to some value other than zero, well, you know this before hand, you might not know what X is however, until runtime. Just as you would with a loop that ends on some condition, you will set X and then and only then call the function, does that make sense?

If you need something to be done, before doing something else, then just don't do something else until something gets done.

Last edited on
Hi all, thanks for your comments on this. Practically speaking, in my program I could easily get over this since the beginning, I am actually using a sort of user validation right when the information are inserted, so there is not the possibility the field is empty (it might be wrong but not empty) and then I simply that value to the member before calling the function (here I am using some setters instead of pubic members)
1
2
3
4
5
6
7
8
9
remote = new SyncHandler;

remote->setRemoteHost(settings.value("remoteHost").toString());
remote->setSourceDir(settings.value("srcDir").toString());
remote->setDestDir(settings.value("destDir").toString());

// verify if remote host is available (ping)
if ( remote->verifyConnection() != 0 ) {
   // throw an error and exit 


So far so good, I could actually cope with this. But question came in mind when I started to think about abstraction. I will never happen but what if anyone else wants to use my class for his code? He does not care about the logic in my class, it just has to work, however in my code there are these conditions of dependencies which need to be satisfied (call f() only after defining X member) otherwise it simply won't work.

Is it common in C++? What are the suggestion? Simply trying to avoid this dependencies when possible? What when it is not possible at all?

The solution of settings a common or fake value like the loopback interface came in my mind as well but it is good I think.

Instead, I seem to understand this is the right way to go for:

Just as you would with a loop that ends on some condition, you will set X and then and only then call the function, does that make sense?

but unfortunately I do no understand how in my case.

Also I see that I could just put a check in the function body, like iHutch105 said, and I think this is pretty much easy to get. I could simply say:
1
2
3
4
5
6
7
8
9
10
11
12
13
// Functions
int MyClass::MyFunction()
{
   if (x == 0) // it has not been initialized yet
   {
      return -1;
   }
   else
   {
      // Do stuff here
      return 0;
   }
}


and then for example simply document that MyFunction() requires X to be initialized else it will return a value -1. Does it work?

So I am really just trying to understand what is rule to follow in this case and what must be avoided in order to not break this rule.
Topic archived. No new replies allowed.