-3

Helo, I'm new to C++ programming. I'm not understanding about these line of code...

SomeClass someClassObject(35);
...
someClassObject = SomeClass(46);

first of all,what does "someClassObject = SomeClass(46)" does? Is it like what pointer of SomeClass E.G.: "someClassObject = new SomeClass(46)" does but on stack?

  • `someClassObject = new SomeClass(46);` would be illegal C++, stackoverflow isn't really meant for questions so broad and with likely so many dupes, please take a look at [this](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – George Jan 10 '17 at 14:05
  • the first is declaring a user defined object calling the constructor that takes an integer. the second you called the overloaded assignment operator. – Raindrop7 Jan 10 '17 at 14:06

3 Answers3

2

No pointers are involved here. SomeClass(46) constructs a temporary object, typically on the stack. This is followed by an assignment to the variable someClassObject. The exact semantics of this assignment can be user-defined by overloading the assignment operator.

PS: Note the missing semicolons at the end of your statements.

Thomas B Preusser
  • 1,159
  • 5
  • 10
1

SomeClass(46) constructs a new instance of SomeClass on the stack, by calling the constructor passing the number 46 to it.

someClassObject = some instance of SomeClass call the operator= on someClassObject.

Unless the constructor is declared explicit it could also have been written as:

someClassObject = 46;

Take a look at this example: http://ideone.com/7kgWob pasted below:

#include <iostream>
using namespace std;

class SomeClass
{
private:
    int i = 0;

public:
    SomeClass() { cout << "default constructor\n"; };
    SomeClass(int val) { i = val; cout << "constructor getting int: " << val << '\n'; };
    ~SomeClass() { cout << "destrucing object having i: " << i << '\n'; };

    SomeClass& operator=(const SomeClass& rhs) { 
        cout << "operator= getting int: " << rhs.i << '\n';
        if (this != &rhs) {
            i = rhs.i;
        }
        return *this;
    }
};

int main() {
    SomeClass a(10);
    SomeClass b = SomeClass(20);
    SomeClass c(35);
    c = SomeClass(46);
    return 0;
}

It will output:

constructor getting int: 10
constructor getting int: 20
constructor getting int: 35
constructor getting int: 46
operator= getting int: 46
destrucing object having i: 46
destrucing object having i: 46
destrucing object having i: 20
destrucing object having i: 10

In essence it creates a temporary instance, and sets the current instance to the same value as the temporary (two objects with the same value exists at this point). It then frees the temporary. Since no new is involved it happens on the stack.

Tommy Andersen
  • 7,165
  • 1
  • 31
  • 50
0

SomeClass* someClassObject = new SomeClass(46); you initialized a pointer of type class SomeClass to a dynamic object through calling the constructor that takes an integer.

the second you called the overloaded assignment operator which calls the constructor that takes an integer creating a temporary object thenn assigns it to someClassObject.

don't forget ; it is not arbitrary.

Raindrop7
  • 3,889
  • 3
  • 16
  • 27