Solved

Get/Set functions

Posted on 2006-07-13
13
271 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

770 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