Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to access the public methods of a private data member?

Reply
Thread Tools

how to access the public methods of a private data member?

 
 
Neil
Guest
Posts: n/a
 
      03-06-2007
I am trying to access the public methods of a private data member in
the composition. The following is my code. Am I right? If the return
value of function in class Outer 'getInner()' is not reference, I
cannot change the data member ia, why?

//outer.h
class Outer {
class Inner {
int ia;
public:
Inner(int val=0):ia(val) {}
int get() const {return ia;}
void set(int val){ia=val;}
}in;
public:
Outer(int ival=0):in(ival){}
Inner& getInner() {return in;} //What if Inner getInner(){...}
};

//main.cpp
#include <iostream>
#include "outer.h"
using namespace std;

int main(){
Outer out;
int a;
a=out.getInner().get();
cout<<"a="<<a<<endl;

out.getInner().set(124);
int b;
b=out.getInner().get();
cout<<"b="<<b<<endl;


return 0;
}


Many thanks!

 
Reply With Quote
 
 
 
 
Tigera
Guest
Posts: n/a
 
      03-06-2007
If you return something by value, you get a copy of the value. It
doesn't matter whether the return type is an inner class or not.
Though it surprises me that the code even compiles without the
reference - I'd have thought the compiler would have complained of an
incomplete type.

 
Reply With Quote
 
 
 
 
Neil
Guest
Posts: n/a
 
      03-06-2007
On Mar 5, 9:12 pm, "Tigera" <(E-Mail Removed)> wrote:
> If you return something by value, you get a copy of the value. It
> doesn't matter whether the return type is an inner class or not.
> Though it surprises me that the code even compiles without the
> reference - I'd have thought the compiler would have complained of an
> incomplete type.


Thank you!

If I don't return by reference, does that mean it will create a copy
of object Inner when 'out.getInner().set(124);' and create another
copy of object Inner when 'b=out.getInner().get();'?

So, even I reset the value, the new copy of object Inner is still
initialized by 0, is that right?

Neil

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd loyola MCSE 4 11-15-2006 02:40 AM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd loyola Microsoft Certification 3 11-14-2006 05:18 PM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd loyola MCSD 3 11-14-2006 05:18 PM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd realexxams@yahoo.com Microsoft Certification 0 05-10-2006 02:35 PM
microsoft.public.dotnet.faqs,microsoft.public.dotnet.framework,microsoft.public.dotnet.framework.windowsforms,microsoft.public.dotnet.general,microsoft.public.dotnet.languages.vb Charles A. Lackman ASP .Net 1 12-08-2004 07:08 PM



Advertisments