|
-
Sep 26th, 2001, 01:38 PM
#1
Thread Starter
transcendental analytic
Nested classes
Why doesn't this work unless i comment out "public:" before class aa?
Code:
class a{
//public:
class aa{
public:
aa(){};
aa(int x,int y){};
};
class ab{
aa _aa;
public:
ab(){_aa=aa(1,2);};
};
};
It complains that it can't access underlined line from with the bold statement.
Quote from http://www.icce.rug.nl/docs/cplusplus/cplusplus16.html
The constructor SecondWithin() and the member function getVar() of the class SecondWithin can also only be reached by the members of Surround (and by the members of its nested classes).
Which seems not to be the case in VC++
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Sep 26th, 2001, 03:03 PM
#2
This ONLY works if i dont comment out the "public:". If I do:
Code:
...
class ab{
aa _aa;
public:
ab(){aa abc(1, 2);_aa=abc;};
};
...
It works fine, commented public: or not.
Z.
-
Sep 26th, 2001, 03:15 PM
#3
Thread Starter
transcendental analytic
Okay that's one way around it, thanks, but will the compiler be smart enough to not make a copy of it?
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Sep 26th, 2001, 03:23 PM
#4
I doubt it, but it will be removed when the constructor exits. You could also declare _aa as an aa*, and use "new" in the constructor for ab (ie, _aa = new aa(1, 2)), then delete _aa in ab's destructor.
Z.
-
Sep 26th, 2001, 03:32 PM
#5
Thread Starter
transcendental analytic
Originally posted by Zaei
I doubt it, but it will be removed when the constructor exits. You could also declare _aa as an aa*, and use "new" in the constructor for ab (ie, _aa = new aa(1, 2)), then delete _aa in ab's destructor.
Z.
I wouldn't go for any freaky solutions A pointer would be plain painful overhead. I was rather wondering if the local variable had to be instantiated at all but instead assign aa immediately. I know the compiler tries to use the registrers as efficient as possible, but I have no guarantee.
Anyways, thanks for your solution, I still though await a "better" less awkward solution
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Sep 26th, 2001, 04:22 PM
#6
Ah, figured it out. Make "class aa" a friend of ab:
Code:
class ab;
class aa
{
friend class ab;
...
};
class ab
{
...
public:
ab() { _aa = aa::aa(1, 2);}
};
Z.
-
Sep 26th, 2001, 05:07 PM
#7
Thread Starter
transcendental analytic
Originally posted by Zaei
Ah, figured it out. Make "class aa" a friend of ab:
Code:
class ab;
class aa
{
friend class ab;
...
};
class ab
{
...
public:
ab() { _aa = aa::aa(1, 2);}
};
Z.
What a nasty little solution you came up with I had tried similar but forgot to put class ab; on top.
But I don't like it as said, It's against my moral, I clearly see that this undermines OOP, aa shouldn't need to specify which classes it can be instantiated from.
Well thanks anyway
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Sep 26th, 2001, 05:14 PM
#8
Only if aa is not public in a. If it was, you could instantiate it like so:
Code:
aa _aa;
...
_aa = a::aa::aa(1, 2);
If not, you can make the classes friends, and get by the private/public thing.
Z.
-
Sep 26th, 2001, 05:26 PM
#9
Thread Starter
transcendental analytic
Man I'm happy to find a reasonable solution!
Code:
class a{
class aa{
public:
aa(){};
aa(int x,int y){};
};
class ab{
typedef aa aa2;
aa _aa;
public:
ab(){_aa =aa2(1, 2);};
};
};
Allthough it still is annoying, I would call it OOP at least
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Sep 26th, 2001, 05:28 PM
#10
Thread Starter
transcendental analytic
Whoa!
typedef aa aa;
is even better
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Sep 26th, 2001, 05:51 PM
#11
-
Sep 27th, 2001, 09:51 AM
#12
Have you tried this ab constructor?:
Code:
ab() : _aa(1, 2)
{}
This is the clean way of doing such things.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 27th, 2001, 10:57 AM
#13
Thread Starter
transcendental analytic
Yes, but that only works for the contructor. In the project i'm developing I rather got stuck with the calls to contructors of classes at same level, within the class but not in its own constructor.
A qwestions since you seem to know pretty much, what's the actual difference after compilation between
ab() : _aa(1, 2){};
and
ab() {_aa=aa(1, 2)};
?
Thanks.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Sep 27th, 2001, 11:54 AM
#14
ab() {_aa=aa(1, 2)};
creates _aa with the standardconstructor, creates a temporary aa object with the (x, y) constructor and assigns it to _aa.
ab() : _aa(1, 2){};
executes the (x, y) constructor for _aa
if you didn't have a standard constructor, but only the (x, y), you couldn't use the first method.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 27th, 2001, 12:08 PM
#15
Thread Starter
transcendental analytic
CornedBee> Why do I get
"error C2064: term does not evaluate to a function"
when I do the following:
aa x;
x(2,3);
I also tried:
aa x;
x.aa(2,3);
but get "error C2274: 'function-style cast' : illegal as right side of '.' operator"
Does this mean I can only call constructors from other constructors?
Isn't the compiler smart enough to compile x=aa(2,3); so that a temporary variable won't be instantiated?
Thanks.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Sep 27th, 2001, 01:01 PM
#16
a) You can call the constructor only on construction time.
This line:
aa x;
already calls the constructor (the one without arguments).
Therefore, neither x(1, 2) nor x.aa(1, 2) can work because the object is already constructed.
You can, however, write
aa x(1, 2);
as this calls the (x, y) constructor instead of the () one.
b) I'm not sure if compilers optimize this. You can look at the asm code to find out (compare with and without optimization).
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 27th, 2001, 01:15 PM
#17
Thread Starter
transcendental analytic
Thanks again I've witnessed that whole classes never got instantiated even if they were used to evaluate an expression, (an article at flipcode.com) based on that i would guess that the compiler nowadays are smart enough, but I still have no guarantee. I need to generate some assembly listings
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|