A Very Simple C++ Code

This is a discussion on A Very Simple C++ Code within the C++ Programming forums, part of the General Programming Boards category; Code: class A{ public: A(){} int x; }; class B: public A{ public: B(){} }; void Test(){ A &a = ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    A Very Simple C++ Code

    Code:
    class A{
    public:
    	A(){}
    	int x;
    };
    
    class B: public A{
    public:
    	B(){}
    };
    
    void Test(){
    	A &a = B();
    	A *p = &B();
    }
    
    int main(){
    	
    	Test();
    	return 1;
    }
    The question is: What's wrong in the Test() ?

    I was told that
    (1) at line: A &a = B(); B() is temporary so I need to change it to ' const A &a = B();'
    So that I can use a in the future.

    (2) A *p = &B(); is correct.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The second is definetely wrong as you'll get a pointer to an object that is destroyed immediately.

    The first seems to allow a const reference and the destructor isn't called until a goes out of scope.

    Depending on what you are trying to do, I'd rather use
    Code:
    A *p = new B;
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    I compiled the code and it works. Wierd...

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    That's because it doesn't do anything. It may work as in - it needn't crash.

    Let's add some flesh:
    Code:
    #include <iostream>
    #include <string>
    
    class A{
    public:
    	A(int n): s("Hello world"), x(n)
        {std::cout << "Constructor: " << x << '\n';}
        ~A() {std::cout << "Destructor: " << x << '\n';}
    	std::string s;
    	int x;
    };
    
    class B: public A{
    public:
    	B(int n): A(n){}
    };
    
    void Test(){
    	const A &a = B(1);
    	std::cout << a.s << '\n';
    	A *p = &B(2);
    	std::cout << p->s << '\n';
    }
    
    int main(){
    	
    	Test();
    	return 1;
    }
    This compiles with a warning: [Warning] taking address of temporary
    My output:
    Code:
    Constructor: 1
    Hello world
    Constructor: 2
    Destructor: 2
    
    Destructor: 1
    See that it failed to print "hello world" the second time (could have crashed at that place).

    The const reference thing is probably there to allow you to call functions with temporary objects:
    Code:
    void foo(const MyClass& m) {}
    ...
    foo(MyClass(parameters));
    If you want to have a pointer to the base class, see my previous post.
    Last edited by anon; 06-06-2007 at 08:54 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Thank you, anon. Where are the B() allocated?

    I guess in
    A *p = &B();
    B() is allocated on stack? (Seems it is freed after this line, not after Test() function?)

    Then in
    A *p = new B();
    B() is allocated on heap?

    ?

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Probably.

    Just forget about the first version because p will become invalid immidiately and you won't be able to use it anyway.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    And it wouldn't compile anyway. You can't take the address of a temporary.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 11-23-2005, 07:53 AM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  3. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM
  4. Simple Code, looking for input.
    By Alien_Freak in forum C Programming
    Replies: 3
    Last Post: 03-03-2002, 10:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21