-3

I have started doing C++ clases for over 2 weeks now and I have a small problem that i dont know how to solve. I have to make a simple Class with one int parameter. That class needs to work with direct and copying initilaization and needs to print message which initialization is used. For example:

TestClass T(60) //this should print a message "Direct initialization"

TestClass T = 205 // this should print a messagte "Copying initialization"
Blastfurnace
  • 18,411
  • 56
  • 55
  • 70
  • Think of the type of constructor that is called in each case. https://learn.microsoft.com/en-us/cpp/cpp/constructors-cpp?view=vs-2019 introduces constructors if you are not aware of different kinds of constructors in C++. – Empty Space May 17 '20 at 15:53
  • 2
    Please ask an actual question. What problems did you encouter? – Lukas-T May 17 '20 at 15:58
  • 1
    Can you elaborate exactly what material was taught in, supposedly, this formal C++ class, before you were given this assignment? @MutableSideEffect -- feel free to try it yourself and observe that the same constructor gets called, value assignment in initialization is equivalent to plain ol' construction. That's not the root issue here. – Sam Varshavchik May 17 '20 at 15:59
  • Well after making explicit TestClass(int a) { number = a; std::cout << "Direct initialization" }; I have solved the problem for direct initialization but the problem is I dont know how to make any working constructor with copying initialization. I have tried 10000 things but none of them wored :/ I know that it is because of "explicit" thing but I really dont know how to prevent these two from mixing with each other – JohnWick22 May 17 '20 at 16:01
  • 1
    the compiler generates a = operator from the copy constructor. what is happening is that it is being implicitly converted to a TestClass, then copied to the original TesetClass – Ruiqi Li May 17 '20 at 16:06
  • I have used the same "trcik" with double thing but i have to use "int" type strictly :/ – JohnWick22 May 17 '20 at 16:06
  • 1
    Please provide a [mre] with your full code and any error messages you encounter – Alan Birtles May 17 '20 at 16:20

1 Answers1

1

If you use a constructor with a single variable initialization, then this assignment operation will be done for you automatically. See my code below:

#include<bits/stdc++.h>

using namespace std;

class A {
    int x;

public:
    A(int xx) {
        cout << "Constructor Called" << endl;
        x = xx;
    }

    print() {
        cout << "Value of obj is " << x << endl;
    }
};

int main() {
    A v = 10;
    v.print();
}

Because you used initialization constructor, = operator was handled automatically. To prove that it is using the constructor, I used cout in constructor and created and called another printing function.

Sajib
  • 404
  • 3
  • 15
  • If you are satisfied with the answer, please accept the answer and upvote. :) – Sajib May 17 '20 at 16:09
  • I hope you read this part "That class needs to work with direct and copying initilaization and needs to print message which initialization is used. For example: TestClass T(60) //this should print a message "Direct initialization" TestClass T = 205 // this should print a messagte "Copying initialization"" – JohnWick22 May 17 '20 at 16:19
  • 4
    Avoid posting an answer to questions that are almost sure to close due to lack of information or proper SO formatting guidelines. Better post a comment to guide the OP on how to ask. – Michael Chourdakis May 17 '20 at 16:23
  • @MichaelChourdakis I have just started writing answers. thanks for the heads up. :) – Sajib May 17 '20 at 16:25
  • 1
    https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h might also be interesting for you. Including the whole standard library in a non-portable way instead of just the `iostream`-header is quite a waste, isn't it? Best you just forget this header ever existed ;) – Lukas-T May 17 '20 at 17:18
  • @churill thanks for pointing it out. It is a bad habit of mine which grew during participating in competitive programming contests so that I won't have to think about adding headers later. – Sajib May 17 '20 at 17:22