Results 1 to 17 of 17

Thread: Nested classes

  1. #1

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Angry 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.

  2. #2
    Zaei
    Guest
    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.

  3. #3

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  4. #4
    Zaei
    Guest
    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.

  5. #5

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  6. #6
    Zaei
    Guest
    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.

  7. #7

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  8. #8
    Zaei
    Guest
    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.

  9. #9

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  10. #10

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  11. #11
    Zaei
    Guest
    Nice one!

    Z.

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  13. #13

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  14. #14
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  15. #15

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  16. #16
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  17. #17

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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
  •  



Click Here to Expand Forum to Full Width