Get/Set functions

Hi there,

This is probably a fairly basic question. My program is made up of a number of classes. In my main, I used to have something like this:

int main()
{
  int value = 2;

  ClassA ObjectA;

  ObjectA.ObjectB.ObjectC.ObjectD =  value;

  return 0;
}

Then I changed it to using accessor functions instead and now have this:

int main()
{
  int value = 2;

  ClassA ObjectA;

  ObjectA.GetObjectB().GetObjectC().SetObjectD( value );

  return 0;
}


However now I when I do "cout<<ObjectA.GetObjectB().GetObjectC().GetObjectD()<<endl;"  I just get garbage!!! Why does it not work with the accessor functions!!?

Thanks kindly for your help!!
lost_bits1110Asked:
Who is Participating?
 
smidgie82Commented:
Hi lost_bits1110,

Without seeing the class definitions and the implementation of your accessors, it's impossible to say for sure.  But my first guess is that your accessor functions are returning a copy of the object you want, rather than a reference.  For instance, if you defined the accessor functions similar to the following:

ClassB ClassA::GetObjectB()

then this might be the case.  Try changing this to
ClassB& ClassA::GetObjectB().

If you use the first definition, a copy of ObjectA.ObjectB is created.  ditto for the GetObjectC() call.  so, when you call SetOjbectD, it's setting that value in a temporary object of type class ClassC, which is then immediately discarded.

Cheers!
0
 
rajeev_devinCommented:
Post your classes also.
0
 
lost_bits1110Author Commented:
Hi okay sorry about that, so my classes are defined in a very simple manner, as are my accessor functions.

Below is pretty much how all my classes and their accessor functions are defined.

Class A {

public:
     
        inline int GetObjectA(){ return A; }
        inline void SetObjectA( int a ){ A = a; }

private:
       
       int A;
}
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
smidgie82Commented:
This should generate a compiletime error, as the result of GetObjectA() is an int, which doesn't have member function GetObjectB().  Could you please elaborate more?
0
 
rajeev_devinCommented:
>> Class A {
>>      int A;
your class is A and also your member variable is A.
Do you think this will work ?
It will never work.
0
 
lost_bits1110Author Commented:
OH thats my mistake sorry bout that, its actually this:

ClassA {
public:
        inline ClassB GetObjectB(){ return B; }
private:
        ClassB B;
}


ClassB {
public:
        inline ClassC GetObjectC(){ return C; }
private:
        ClassC C;
}


ClassD {
public:
        inline void SetObjectD( int d ){ D = d; }
private:
        int D;
}
0
 
lost_bits1110Author Commented:
yikes, yet another typo. Please note that in my previous post, there should be the word "class " before every class definition, i.e. class ClassD{ ---- }

Sorry about the confusion.

smidgie82, thanks for your earlier reply. I tried your suggestion and it seemed to work..! Why do you think it would return just a copy rather than a reference!!! I thought it should automatically return the reference? Or do they always just return a copy? If so.. I didnt know this :s  
0
 
rajeev_devinCommented:
If I would have written the classes for these statements

int value = 2;
ClassA ObjectA;
ObjectA.GetObjectB().GetObjectC().SetObjectD( value );

it would have been something like this

class ClassD
{
public:
      ClassD(){}
      ClassD(int i) {}
};

class ClassC
{
public:
      ClassD GetObjectD(){ return m_D; }
      void SetObjectD(ClassD d) {m_D = d;}
private:
      ClassD m_D;
};

class ClassB
{
public:
      ClassC GetObjectC(){ return m_C; }
private:
      ClassC m_C;
};

class ClassA
{
public:`
      ClassB GetObjectB(){ return m_B; }
private:
      ClassB m_B;
};
0
 
smidgie82Commented:
By default, a copy is _always_ returned.  That's one way that encapsulation is guaranteed, because accessors can return copies of members, so that calling functions are prevented from accessing and modifying object internals.  Except when reuse of the copy is necessary, it's typically faster to return a reference, because no data duplication takes place.  And if you don't want any modification of that reference to take place, you can return a constant reference:

const ClassB& ClassA::GetObjectB();

Cheers!
0
 
lost_bits1110Author Commented:
THanks for the tip Rajeev, I know it looks confusing the way I've posted it.
0
 
lost_bits1110Author Commented:
Oh okay thanks smidgie82!!

Now I feel like the purpose of having my Get/Set functions is useless, since now they will be returning references.. :s

OH well
0
 
smidgie82Commented:
Depends.  If you want to be able to differentiate between constant and non-constant instances of your classes, then there's a point, because you can declare one version:

ClassB& ClassA::GetObjectB();

for non-constant instances, and another:

const ClassB& ClassA:GetObjectB() const;

To return constant instances that can't be modified (and neither can their members).  And by using the accessor function, you can offer a consistent interface for multiple uses, which isn't possible by just referencing public data members.
0
 
lost_bits1110Author Commented:
ah okay thats a good point.. I could definately make use of the const in my case,
thanks so much again for your help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.