Solved

Get/Set functions

Posted on 2006-07-13
13
252 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
  • 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now