Thread: How C++ Objects are referenced

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. The code
    Code:
    myObj = new object_type();
    is illegal, so the program did not compile, and execution cannot happen.

    2. In myFunctionReference, no copying is performed by the call. The assignment operator is called inside the function. In myFunctionArgument, arg1 is constructed "during" the function call (i.e., before the flow of control is handed off to the function), then myObj is changed by the assignment operator, then arg1 is destructed before flow of control returns to the caller.

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For the first, you need a pointer. At the declaration, the constructor for object_type* (NOT object_type!) will be called.
    The constructor for object_type will be called at the
    MyObj = new object_type()
    line. Then the address of that object (object_type*) will be assigned to MyObj. This will call the built-in assignment operator for object_type*.
    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.

  3. #18
    Registered User
    Join Date
    Jul 2011
    Posts
    27
    Quote Originally Posted by tabstop View Post
    1. The code
    [code]
    In myFunctionArgument, arg1 is constructed "during" the function call (i.e., before the flow of control is handed off to the function), then myObj is changed by the assignment operator, then arg1 is destructed before flow of control returns to the caller.
    Thanks!! That was the answer I was looking for. So it seems passing objects without references is a much object-oriented and cleaner way of doing things. On the other hand, it's not very efficient.
    In Java, function calls are passed by reference 1) to make things more efficient 2) elminate the confusion for the programmers.

    Also yes I had a few typos. the myObj = new object_type would not have compiled. I meant to say this:
    Code:
    object_type myObj; //global delcaration because I need to initialize it within the case statement in WM_CREATE.
    ...
    WM_CREATE:
    object_type myObj= object_type(dcHandle);
    It just occured to me that when I declare it, a copy was made. At the WM_CREATE, I have the object constructed it again. Yes one would argue that poniters would make things more efficient but I have to keep track of the dcHandle. Anyhow I'll just use pointers and add a destructor.
    Last edited by derder; 08-01-2011 at 09:59 AM.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by derder
    So it seems passing objects without references is a much object-oriented and cleaner way of doing things.
    That is not really true. Inheritance and polymorphism form a fairly significant part of object oriented programming, yet OO polymorphism in C++ takes place through pointers and references. If your parameter is of a base class type, and not a pointer or reference thereof, you would get type slicing instead of the (presumably) desired polymorphism.

    Quote Originally Posted by derder
    In Java, function calls are passed by reference 1) to make things more efficient 2) elminate the confusion for the programmers.
    No, in Java, function arguments are passed by value, but because the variables concerned are of Java reference types, you get a call by reference mechanism. Java reference types are actually more like pointers without pointer syntax.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-01-2009, 04: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, 04:43 PM