Thread: strange!

  1. #1
    Banned
    Join Date
    Nov 2007
    Posts
    678

    strange!

    Code:
    #include <iostream>
    using namespace std;
    
    class Alpha
    {
    public:
    	int value;
    	Alpha() { cout << "Alpha!" << endl; value = 100; }
    };
    
    class Beta
    {
    public:
    	Alpha alpha;
    };
    
    Beta beta;
    int main()
    {
    	Beta beta;
    	cout << beta.alpha.value << endl;
    	return 0;
    }
    // Output:
    // Alpha!
    // Alpha!
    // 100
    When is the Alpha constructor called here?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Before the beta constructor is called.

    Edit: by the way, it's called twice. Once before main is called, and once inside main - as you have two variable declarations "Beta beta;" - one global and one in main.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    When is the Alpha constructor called here?
    In both the cases when the Beta objects named beta are instantiated.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    why would you do this

    public:
    Alpha alpha;

    why not just make it

    class Alpha : Beta

    but you would have to change the code around so Beta class is above Alpha

    Code:
    #include <iostream>
    using namespace std;
    
    class Beta : Alpha
    {
    public:
    	Alpha alpha;
    };
    
    class Alpha
    {
    public:
    	int value;
    	Alpha() { cout << "Alpha!" << endl; value = 100; }
    };
    
    
    
    Beta beta;
    int main()
    {
    	Beta beta;
    	cout << beta.value << endl;
    	return 0;
    }
    // Output:
    // Alpha!
    // Alpha!
    // 100
    Last edited by Anddos; 03-27-2008 at 06:12 AM.

  5. #5
    Banned
    Join Date
    Nov 2007
    Posts
    678
    A global variable declaration:
    Beta beta;
    This executes a full fledged function! And it can do anything allocate memory etc.
    In Java this would have no effect. Only after doing this:
    beta = new Beta();
    It would consume some resources!
    So I am a little confused here!

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    C++ is not Java.

    In Java, every object-type variable is a reference. You must allocate an object using new to get an object.
    In C++, an object-type variable actually is the object, so the object is created "immediately".
    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

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    you can use new on a c++ class object if you make it as a pointer

    Beta *beta = new Beta;
    then use beta->member

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Global construction is part of C++. So you can have global variables, and they are constructed at some point before main is called.

    What particular part are you confused about?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Banned
    Join Date
    Nov 2007
    Posts
    678
    So may be compiler calls the constructors (in case of global objects) before starting main.
    Now the point is:
    Code:
    class Beta
    {
    public:
    	Alpha alpha;
    };
    If the above code was in a .h file. Then?
    They say .h file should not contain any executable stuff?? What is happening here then?
    Also we are supposed to redeclare the static variables out side of .h files? Why? Is the situation not the same here?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A header is not a special file. It can contain executable code, but it shouldn't, because it creates problems when including it (duplicate code).
    If you put a global variable inside a .h file and you include it from two cpp files, you will get a linker error because the same symbol (variable) is defined twice.
    Last edited by Elysia; 03-27-2008 at 06:27 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If the above code was in a .h file.
    It should be in a header file to begin with, unless you are doing some experimentation that has the entire test program in a single source file.

    They say .h file should not contain any executable stuff?? What is happening here then?
    The class named Beta is defined with a member variable named alpha of type Alpha. No objects are created, though if necessary the compiler does generate a default constructor, copy constructor, copy assignment operator and destructor.

    Also we are supposed to redeclare the static variables out side of .h files? Why? Is the situation not the same here?
    No static or global variables are being declared here. A non-static member variable is declared, but it will only exist when a Beta object is instantiated.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Thanks! But still you did not answer it directly. Or may be I am a little confused.
    But when exactly will be the Alpha constructor called?
    Is it called only when we do this:
    Beta beta;
    somewhere??
    And ...
    Alpha alpha;
    inside Beta has no effect at all?

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Because beta has an alpha object, when you create beta, it creates an alpha.
    And when an alpha object is created, the alpha constructor is called.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    But when exactly will be the Alpha constructor called?
    Is it called only when we do this:
    Beta beta;
    somewhere??
    No, it is called whenever an Alpha object is created. It so happens that Beta has an Alpha member variable, so when a Beta object is created, an Alpha object is created.

    And ...
    Alpha alpha;
    inside Beta has no effect at all?
    Yes, at least other than the important "effect" of declaring that Beta has an Alpha member variable named alpha.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Remember that a constructor "constructs" an object, so whenever an object of type alpha, beta, are created, the constructor is called.
    And since there is an alpha member in beta, the alpha member must be constructed, so the constructor is called for alpha whenever you create an beta object.
    Similarly, the constructor for each of the other members would be called, as well. Though built-in types typically has no constructor.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange error -- COM DLL is not installed correctly?
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 07-16-2007, 08:32 AM
  2. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  3. Strange response from MSVC
    By VirtualAce in forum Game Programming
    Replies: 2
    Last Post: 04-17-2004, 07:40 AM
  4. Very strange bug...
    By JaWiB in forum Tech Board
    Replies: 6
    Last Post: 04-27-2003, 01:56 PM
  5. bcc32 compiling error (really strange!!)
    By jester in forum C++ Programming
    Replies: 14
    Last Post: 01-26-2002, 04:00 PM