Click here to Skip to main content
15,353,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
If we declared an Object/Class, such as;
C++
ClassOne classOne;

with a class definition of;
C++
//Figure #1
class ClassOne {

}

//Figure #2
class ClassOne {
    ClassOne(int, int);
}

//Figure #3
class ClassOne {
    ClassOne(const ClassOne&);
}

//Figure #4
class ClassOne {
    ClassOne(ClassOne&&);
}

//Figure #5
class ClassOne {
    //ctor(V)
    //ctor(const ClassOne&)
    //ctor(ClassOne&&)
    //ClassOne& operator=(const ClassOne&)
    //ClassOne& operator=(ClassOne&&)
}

//Figure #6
class ClassOne {
    ...
    ClassOne(int x, int y);
    ClassOne operator+(const ClassOne& classOne_0) const {
        ClassOne classOne;
        classOne.x = (this->x) + classOne_0.x;
        classOne.y = (this->y) + classOne_0.y;
        return classOne;
    }
}
What and who's ctor signature it will be?

What I have tried:

For example at figure #6;
inside of this overloaded operator+()
C++
ClassOne operator+(const ClassOne& classOne_0) const {
        ClassOne classOne; //REM: Why did this instance select the define `ctor(int, int)`
        classOne.x = (this->x) + classOne_0.x;
        classOne.y = (this->y) + classOne_0.y;
        return classOne;
}

And here's main Entry:
C++
int main() {
    ClassOne c1(4,2);
    ClassOne c2(2,4);
    CLassOne c3 = c1 + c2;  //REM: `c1 + c2` become temp-var or `rval ref`
                            //REM: if there's no signature for `rval ref` look 
                            //REM: for `lval ref to const`
    ...
}



"You need to show us your actual code. This won't even compile, because there is no ; at the end of Figure #1, which is an empty class definition for which the compiler will generate all the trivial, default member functions."
C++
#include <iostream>

class ClassOne
{
private:
    int x,
        y;
public:
    ClassOne( int x = 0, int y = 0); // constructor
    ClassOne operator+(const ClassOne&) const; // operator+()
    
    int getX() const 
    {
        return this->x;
    }
    
    virtual ~ClassOne()
    {
        std::cout << "virtual ~ClassOne(V)V\n";
    }
};


ClassOne::ClassOne( int x, int y )
{
    std::cout << "ClassOne(I,I)V\n";
    this->x = x;
    this->y = y;
}


ClassOne ClassOne::operator+ (const ClassOne& classOne_0) const
{
    std::cout << "------------: A what?\n";
    ClassOne classOne; //REM: ???
    std::cout << "------------\n";
    classOne.x = (this->x + classOne_0.x);
    classOne.y = (this->y + classOne_0.y);
    return classOne;
}

int main()
{
    ClassOne c1(4,2);
    ClassOne c2(2,4);
    ClassOne c3 = c1 + c2;
    std::cout << c3.getX() << std::endl;
} 
Posted
Updated 27-May-22 1:26am
v4

You don't believe in experimental approach, do you?
First fix the code (for instance the C++ keyword is class, instead of Class) and then run it (possibly using the debugger or, at least, introducing statements to produce some meaningful output).
   
Comments
Phoenix Liveon 27-May-22 6:32am
   
Noted. But there was one 'instance' in particular that I couldn't follow or wrap my head around, see @ "What I have Tried".
Phoenix Liveon 27-May-22 6:48am
   
If I could read the generated assembly language flawlessly and without stumbling or loosing my way.
CPallini 27-May-22 8:48am
   
You did write a 'take all' constructor, so, no wonder the compiler selected it.
Phoenix Liveon 27-May-22 8:51am
   
oh! ok! I'll look into it further. Thank you very much!
CPallini 31-May-22 2:12am
   
You are welcome.
You need to show us your actual code. This won't even compile, because there is no ; at the end of Figure #1, which is an empty class definition for which the compiler will generate all the trivial, default member functions.
   
Comments
Phoenix Liveon 27-May-22 7:29am
   
thanks and noted. I've updated the above question.
Greg Utas 27-May-22 7:34am
   
The reason that the ClassOne(int, int) constructor was selected is because both of its arguments specify 0 as a default. So if an instance of the class is declared without arguments for the constructor, that constructor will be selected. I think that if you actually declared a constructor that took no arguments, the compiler would generate an error saying that your code was ambiguous: it wouldn't know which constructor to select, because both could be invoked without arguments.
Phoenix Liveon 27-May-22 7:51am
   
Thanks! There are many things I might learn about the order or precedence of how the compiler selects those special member functions. Um, can we do this to counteract this ambiguous?
    /*explicit*/ ClassOne() : ClassOne(0, 0)  { // ctor delegation
        std::cout << "delegated ClassOne(V)V\n";
    }
    /*explicit*/ ClassOne( int x, int y); // overloaded constructor
Phoenix Liveon 27-May-22 7:53am
   
Is there no creepy crawling butt-biter in this code? Right?
Greg Utas 27-May-22 8:03am
   
Yes, this is unambiguous. But you could just stick to your original constructor, with its two defaults. The only difference is that your original code allows a ClassOne to be constructed with *one* argument (x), with y still defaulting to 0. I don't think that's a useful capability, or even appropriate, so I prefer your new version. But I wouldn't bother to forward from the ClassOne() constructor to the ClassOne(int, int) constructor. I'd just initialize x and y to 0 right in the ClassOne() constructor, which is more efficient.
Phoenix Liveon 27-May-22 8:09am
   
Thank you so much for your direct response and the solution; I now feel more confident in moving forward and learning more.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900