Solved

Get/Set functions

Posted on 2006-07-13
13
290 Views
Last Modified: 2010-04-01
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!!
0
Comment
Question by:lost_bits1110
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
13 Comments
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17100798
Post your classes also.
0
 
LVL 9

Accepted Solution

by:
smidgie82 earned 75 total points
ID: 17100805
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
 

Author Comment

by:lost_bits1110
ID: 17100862
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:smidgie82
ID: 17100979
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
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17101051
>> 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
 

Author Comment

by:lost_bits1110
ID: 17101075
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
 

Author Comment

by:lost_bits1110
ID: 17101113
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
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17101134
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
 
LVL 9

Expert Comment

by:smidgie82
ID: 17101147
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
 

Author Comment

by:lost_bits1110
ID: 17101151
THanks for the tip Rajeev, I know it looks confusing the way I've posted it.
0
 

Author Comment

by:lost_bits1110
ID: 17101170
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
 
LVL 9

Expert Comment

by:smidgie82
ID: 17101207
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
 

Author Comment

by:lost_bits1110
ID: 17101252
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

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question