Thread: class_name ob1 = class_name(5)

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    40

    class_name ob1 = class_name(5)

    Ok, well I know that this statement:

    class_name ob1 = class_name(5);

    calls the class_name(int) constructor, but what I don't understand is how.

    I've been told that such a statement as class_name ob1=5; simply is replaced by the compiler with class_name ob1(5), so I'd assume that class_name ob1 = class_name(5) doesn't invoke any '=' oparator functions...

    I really appreciate it

  2. #2
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Quote Originally Posted by krygen
    Ok, well I know that this statement:

    class_name ob1 = class_name(5);

    calls the class_name(int) constructor, but what I don't understand is how.

    I've been told that such a statement as class_name ob1=5; simply is replaced by the compiler with class_name ob1(5), so I'd assume that class_name ob1 = class_name(5) doesn't invoke any '=' oparator functions...

    I really appreciate it
    I believe saying:

    Cat Frisky(5);

    Is synonymous with something like:

    Cat Frisky = Cat(5);

    Any way you initialize an object in one of these ways, it will invoke an assignment.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I believe saying:

    Cat Frisky(5);

    Is synonymous with something like:

    Cat Frisky = Cat(5);
    Logically, yes, but assume you overload the = operator to have some side effect. If the compiler optimizes that away it will never occur...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    Operator= isn't called, the best matching constructor is.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    In this case, the copy constructor is called.
    Cat Frisky = Cat(5);
    is equivalent to
    Cat Frisky(Cat(5));

    I'm not entirely sure, but I believe that a compiler may optimize the unnamed temporary away.
    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

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    I think it's allowed to optimize the temporary away.

    Where the standard doesn't allow this is for something like

    Code:
    inline void dispatcher(int bla, T x, T y) {
    
    stuff (x, y); //copies required even though all we do is forward.
    
    }
    
    void stuff(T x, T y) { /* bla */ }

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    40
    when you have a statement such as
    class_name ob1 = class_name(5);

    would it be better to think of that as more of the *syntax* for a way to call the constructor, instead of trying to break in into its respective parts? I believe it has nothing to do with the = operator...

    and then classs_name ob1=5; would be just shorthand, optimized by compiler, for that statement? and class_name ob1(5) would be as well?

    not sure if that's really whats happening or not

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Personally, I see no reason why one wouldn't write
    class_name ob1(5);
    in the first place.
    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

  9. #9
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    You can test how it works with this code...
    Code:
    #include <iostream>
    using namespace std;
    
    class TestClass {
        public:
            TestClass( int testValue = 0 ){
                classID = ++count;
                cout << "Constructor: " << classID << " called" << endl;
                cout << "Value passed: " << testValue << endl;
            }
            
            ~TestClass() {
                cout << "Destructor: " << classID << " called" << endl;
            }
            
    
            
        private:
            int testValue;
            int classID;
            static int count;
    };
    
    int TestClass::count = 0;
        
    int main()
    {
        TestClass Class1;
        TestClass Class2(2);
        TestClass Class3 = TestClass(5);
        
        return 0;
    }
    Here's the output.
    Code:
    EXECUTING:
    /home/rjones/projects/testClass
    ----------------------------------------------
    Constructor: 1 called
    Value passed: 0
    Constructor: 2 called
    Value passed: 2
    Constructor: 3 called
    Value passed: 5
    Destructor: 3 called
    Destructor: 2 called
    Destructor: 1 called
    
    ----------------------------------------------
    Program exited successfully with errcode (0)
    Press the Enter key to close this terminal ...
    As you'll see from the output, when TestClass Class3 = TestClass(5); is called, only one constructor is called. If the = were used, then a temporary obj would have been created then copied.
    Last edited by Scribbler; 01-28-2005 at 02:48 PM.

Popular pages Recent additions subscribe to a feed