Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Get/Set functions

Posted on 2006-07-13
13
Medium Priority
?
305 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 300 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

927 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