How C++ Objects are referenced

This is a discussion on How C++ Objects are referenced within the C++ Programming forums, part of the General Programming Boards category; Hello, I have a class that is instantiated as follows: DCManager dcManager; ... WM_CREATE: dcManager= DCManager(dcImage); It seems the compiler ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    27

    How C++ Objects are referenced

    Hello,
    I have a class that is instantiated as follows:

    DCManager dcManager;
    ...
    WM_CREATE:
    dcManager= DCManager(dcImage);
    It seems the compiler complaines that there's no default constructor when I use the following code "DCManager dcManager". This tells me that "DCManager dcManager" instantiates an instance during the declaration which is not I wanted.

    Question 1. In my code above, an instance is created during the declaration, so in my WM_CREATE, another instance is created. That means the first instance is explictly deleted? That is, the default constructor is called when the dcManager=DCManager(dcImage) creates another instance?

    Now, I have my code as follows:
    DCManager dcManager=NULL;
    QueueHolder queueHolder=NULL;
    ...
    WM_CREATE:
    dcManager= DCManager(dcImage);
    queueHolder = QueueHolder(dcManager);
    Question 2: In the code above, is the dcManger passed as a reference or another copy is made when the queueHolder = QueueHolder(dcManager) is called?


    Question 3: I want to debug to see if the deconstructor for the DCManager is coded correctly. Can I explictly use delete the dcManager instead of waiting for it to go out of scope:
    WM_CREATE:
    dcManager= DCManager(dcImage);
    queueHolder = QueueHolder(dcManager);
    WM_DESTROY:
    delete dcManager;
    delete queueHolder;

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I suppose the first question is what else you think "DCManager dcManager" could be other than the creation of a variable/object -- unless you've typedef'ed DCManager to be a pointer type (which is Evil), but then the compiler wouldn't complain, because in that case dcManager wouldn't be an object.

    If there is no default constructor then I don't know how version 1 even compiles. If you created a default constructor, then the last line of the version 1 code would create a temporary object using dcImage, then copy that object into the previously constructed object (assuming lack of Evil as above).

    There is no way to know the answer to question 2, as it depends on the function. "DCManager" and "reference to DCManager" (DCManager&) are different types, and whichever one the function calls for in the prototype is the one it uses. (Again, are DCManager and QueueHolder typedef'ed to pointers?)

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    27
    If you created a default constructor, then the last line of the version 1 code would create a temporary object using dcImage, then copy that object into the previously constructed object (assuming lack of Evil as above).
    Ok, so I added a default constructor after the compiler complains. In the WM_CREATE method, what happens when I create another object dcManager= DCManager? Is the previous dcManager created during my declaration automatically deleted?

    Question 2 can be ignored because I was too vague.

    I am still curious on my question 3. If i don't use the delete dcManager, I know the dcManager will be deleted when it's out of scope. However, can I explicitly delete it? I want to see how the desconstructor remove the device context, and use a few routine to test if the device context are deleted properly.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    Quote Originally Posted by derder
    If i don't use the delete dcManager, I know the dcManager will be deleted when it's out of scope. However, can I explicitly delete it? I want to see how the desconstructor remove the device context, and use a few routine to test if the device context are deleted properly.
    In that case, use delete, but also use new. Actually, it would be better if you used a smart pointer so you get the best of both worlds: if you don't destroy the object, it is destroyed when the smart pointer goes out of scope, while you retain the option of destroying the object before the smart pointer goes out of scope.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    27
    I was too vague on the pass by reference with question 2. Here's what I meant:
    Code:
    public:
     MYObj myObj;
    
    OutputManager::OutputManager(MyObj &_myObjArg) {
    	myObj= _myObjArg;
    }
    Is MYObj objected created during my declaration? Is myObj an object? Is so what happens during assignment such as myObj= _myObjArg?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    Quote Originally Posted by derder
    Is MYObj objected created during my declaration?
    No, that member object is only created when an OutputManager object is created.

    Quote Originally Posted by derder
    Is myObj an object?
    Yes.

    Quote Originally Posted by derder
    Is so what happens during assignment such as myObj= _myObjArg?
    Copy assignment. Under the normal semantics, this means myObj becomes a copy of _myObjArg, or rather the object that _myObjArg refers to.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    27
    No, that member object is only created when an OutputManager object is created.
    If no then why is it in my code

    Code:
    DCManager dcManager;
     ...
     WM_CREATE:
     dcManager= DCManager(dcImage);
    The compiler complains the dcManager needs a default constructor. The error only goes away when
    a) I declared as follows: DCManager dcManager=NULL;
    or b) when I add a default constructor for DCManager.


    Copy assignment. Under the normal semantics, this means myObj becomes a copy of _myObjArg, or rather the object that _myObjArg refers to.
    So this means if my argument is declared &myObjArg, then myObjArg refers to the original object passed in? If my argument is declared as myobjArg then the assignment makes another copy?
    Last edited by derder; 07-28-2011 at 11:45 PM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    Quote Originally Posted by derder
    The compiler complains the dcManager needs a default constructor. The error on goes away when
    a) I declared as follows: DCManager dcManager=NULL;
    Basically, you used a constructor for DCManager other than the default constructor. Passing NULL to the constructor in that way is misleading because dcManager is not a pointer (or pointer-like); you could have written:
    Code:
    DCManager dcManager(0);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Are you coming from a C# background, or some other language?
    For things like this where you clearly have some idea about how these things may work, we can probably help better if we can relate C++ to whatever other language you are familiar with.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    Jul 2011
    Posts
    27
    Quote Originally Posted by iMalc View Post
    Are you coming from a C# background, or some other language?
    For things like this where you clearly have some idea about how these things may work, we can probably help better if we can relate C++ to whatever other language you are familiar with.
    I haven't been programming for 4 years. The language I am thinking off when I wrote the question was c pointers. I can visualize c pointers as follows:
    Code:
    int x=0;
       int *y=&x;
       int z=0;
          x++;
        z=*y;
    x is an address location with value 0. The pointer of Y points to address of x. The value of the pointer y is 0. Let z equals to the value of the pointer y.

    For code such as the following:
    Code:
    public:
     MYObj myObj1;
     MyObj myObj2 = MyObj();
    
    OutputManager::OutputManager(MyObj &_myObjArg) {
    	myObj1= _myObjArg;
    }
    OutputManager::OutputManager(MyObj _myObjArg) {
    	myObj1= _myObjArg;
    }
    I am trying to figure out what myObj1 is. Is it just a reference pointer?
    Last edited by derder; 07-29-2011 at 05:58 AM.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    myObj1 is a real honest-to-goodness thing; no pointers anywhere in the whole thing.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    Quote Originally Posted by derder View Post
    I haven't been programming for 4 years. The language I am thinking off when I wrote the question was c pointers. I can visualize c pointers as follows:
    Code:
    int x=0;
       int *y=&x;
       int z=0;
          x++;
        z=*y;
    x is an address location with value 0. The pointer of Y points to address of x. The value of the pointer y is 0. Let z equals to the value of the pointer y.
    Either you understand the concept and you explain it poorly, or you don't understand at all.
    x is a variable that holds an integer.
    y is a variable that holds a memory address. The memory address pointed to by the value in y holds a 0.
    And so on.

    For code s
    uch as the following:
    Code:
    public:
     MYObj myObj1;
     MyObj myObj2 = MyObj();
    
    OutputManager::OutputManager(MyObj &_myObjArg) {
    	myObj1= _myObjArg;
    }
    OutputManager::OutputManager(MyObj _myObjArg) {
    	myObj1= _myObjArg;
    }
    I am trying to figure out what myObj1 is. Is it just a reference pointer?
    It is neither.
    Consider an example of two integers, x and y.
    First, we assign 0 to x. That is, we set x's state to 0.
    Then we assign 5 to y. That is, we set y's state to 5.
    Then we assign y to x. Now x's state is the same as y, in this example, 5.
    Objects are the same thing, only their "states" cannot be described as integers.
    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.

  13. #13
    Registered User
    Join Date
    Jul 2011
    Posts
    27
    Quote Originally Posted by laserlight View Post
    Basically, you used a constructor for DCManager other than the default constructor. Passing NULL to the constructor in that way is misleading because dcManager is not a pointer (or pointer-like); you could have written:
    Code:
    DCManager dcManager(0);
    I initially asked if objects are created in my declaraton:
    Code:
    DCManager dcManger;
    You said no. However reading your quote above, it seems you need to add a default constructor. This implies that the declaration creates an object. That is why the default constructor is called.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by derder View Post
    I initially asked if objects are created in my declaraton:
    Code:
    DCManager dcManger;
    You said no. However reading your quote above, it seems you need to add a default constructor. This implies that the declaration creates an object. That is why the default constructor is called.
    Context is important. Laserlight told you that when you do this:
    Code:
    class someClass {
        object_type myObj;
    };
    myObj is not constructed until you create an object of type someClass. This is a completely different context of code to
    Code:
    DCManager dcManager;
    which is, well, the creation of a DCManager object. And in fact, it is at this time that myObj (and all the other bits'n'pieces of a DCManager) get created.

  15. #15
    Registered User
    Join Date
    Jul 2011
    Posts
    27
    That's say at the main.cpp, I declare the myObj globally as follows:
    Code:
    object_type myObj;
    Then at the main routine I have the following code and later passed to myFunctionReference and myFunctionArgument:
    Code:
    myObj = new object_type();
    Question 1. How many times is myObj created? If twice, what happens during the new construction? Is it copy-assignment?


    Also if I have to functions called myFunctionReference and myFunctionArgument:
    Code:
    void myFunctionReference(object_type &arg1) {
      myObj=arg1;
    }
    void myFunctionArgument(object_type arg1) {
     myObj=arg1;
    }
    In myFunctionReference, how many copy-assignment took place? Once? Right when the myObj is assigned to arg1.
    In myFunctionArgument, is there two times? arg1 is another copy of object_type and myObj=arg1 makes another copy?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-01-2009, 03:53 PM
  2. Replies: 3
    Last Post: 04-16-2004, 04:19 PM
  3. Replies: 4
    Last Post: 10-16-2003, 11:26 AM
  4. What does de-referenced mean?
    By Gamma in forum C++ Programming
    Replies: 2
    Last Post: 04-14-2002, 11:06 AM
  5. How to change value of a referenced object.
    By sean in forum C++ Programming
    Replies: 4
    Last Post: 12-27-2001, 03:43 PM

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