I've heard that the advantage of using initialization lists in constructor would be that there will be no extra copies of class type objects. But what does it mean for the following code in class T constructor? If i comment the assignment and use initialization lists what would be the difference?
#include <iostream>
using std::cout;
using std::endl;
using std::ostream;
class X {
public:
X(float f_x = 0, float f_y = 0):x(f_x), y(f_y) {}
~X() {}
X(const X& obj):x(obj.x), y(obj.y) {}
friend ostream& operator << (ostream &os, X &obj);
private:
float x;
float y;
};
ostream& operator << (ostream &os, X &obj)
{ os << "x = " << obj.x << " y = " << obj.y; return os;}
class T {
public:
T(X &obj) : x(obj) { /* x = obj */ }
~T() { }
friend ostream& operator << (ostream &os, T &obj);
private:
X x;
};
ostream& operator << (ostream &os, T &obj)
{ os << obj.x; return os; }
int main()
{
X temp_x(4.6f, 6.5f);
T t(temp_x);
cout << t << endl;
}
It's exactly what you already said. If you don't use the initializer list, then the default constructor will get called first, and then the assignment operator will get called.
In your example, this is relatively benign (the compiler may even optimise this, I think). But in other cases, it's simply not possible to avoid the initializer list. Imagine if X didn't have a public assignment operator.
If you use Assignment, then:
x will be default constructed first &
then assigned with obj.
The cost is, Default Construction + Assignment
If you use Member initializer list then:
x will be constructed and initialized with obj.
The cost is, Construction only
T(X &obj) : x(obj) {}
will copy construct x from obj, while
T(X &obj){ x = obj; }
will construct a default x, only to then replace it with the values from obj.
If you intent to construct a member object, you should do it in the initialization list.
Related
myclass is a C++ class written by me and when I write:
myclass x;
cout << x;
How do I output 10 or 20.2, like an integer or a float value?
Typically by overloading operator<< for your class:
struct myclass {
int i;
};
std::ostream &operator<<(std::ostream &os, myclass const &m) {
return os << m.i;
}
int main() {
myclass x(10);
std::cout << x;
return 0;
}
You need to overload the << operator,
std::ostream& operator<<(std::ostream& os, const myclass& obj)
{
os << obj.somevalue;
return os;
}
Then when you do cout << x (where x is of type myclass in your case), it would output whatever you've told it to in the method. In the case of the example above it would be the x.somevalue member.
If the type of the member can't be added directly to an ostream, then you would need to overload the << operator for that type also, using the same method as above.
it's very easy, just implement :
std::ostream & operator<<(std::ostream & os, const myclass & foo)
{
os << foo.var;
return os;
}
You need to return a reference to os in order to chain the outpout (cout << foo << 42 << endl)
Even though other answer provide correct code, it is also recommended to use a hidden friend function to implement the operator<<. Hidden friend functions has a more limited scope, therefore results in a faster compilation. Since there is less overloads cluttering the namespace scope, the compiler has less lookup to do.
struct myclass {
int i;
friend auto operator<<(std::ostream& os, myclass const& m) -> std::ostream& {
return os << m.i;
}
};
int main() {
auto const x = myclass{10};
std::cout << x;
return 0;
}
Alternative:
struct myclass {
int i;
inline operator int() const
{
return i;
}
};
myclass is a C++ class written by me and when I write:
myclass x;
cout << x;
How do I output 10 or 20.2, like an integer or a float value?
Typically by overloading operator<< for your class:
struct myclass {
int i;
};
std::ostream &operator<<(std::ostream &os, myclass const &m) {
return os << m.i;
}
int main() {
myclass x(10);
std::cout << x;
return 0;
}
You need to overload the << operator,
std::ostream& operator<<(std::ostream& os, const myclass& obj)
{
os << obj.somevalue;
return os;
}
Then when you do cout << x (where x is of type myclass in your case), it would output whatever you've told it to in the method. In the case of the example above it would be the x.somevalue member.
If the type of the member can't be added directly to an ostream, then you would need to overload the << operator for that type also, using the same method as above.
it's very easy, just implement :
std::ostream & operator<<(std::ostream & os, const myclass & foo)
{
os << foo.var;
return os;
}
You need to return a reference to os in order to chain the outpout (cout << foo << 42 << endl)
Even though other answer provide correct code, it is also recommended to use a hidden friend function to implement the operator<<. Hidden friend functions has a more limited scope, therefore results in a faster compilation. Since there is less overloads cluttering the namespace scope, the compiler has less lookup to do.
struct myclass {
int i;
friend auto operator<<(std::ostream& os, myclass const& m) -> std::ostream& {
return os << m.i;
}
};
int main() {
auto const x = myclass{10};
std::cout << x;
return 0;
}
Alternative:
struct myclass {
int i;
inline operator int() const
{
return i;
}
};
myclass is a C++ class written by me and when I write:
myclass x;
cout << x;
How do I output 10 or 20.2, like an integer or a float value?
Typically by overloading operator<< for your class:
struct myclass {
int i;
};
std::ostream &operator<<(std::ostream &os, myclass const &m) {
return os << m.i;
}
int main() {
myclass x(10);
std::cout << x;
return 0;
}
You need to overload the << operator,
std::ostream& operator<<(std::ostream& os, const myclass& obj)
{
os << obj.somevalue;
return os;
}
Then when you do cout << x (where x is of type myclass in your case), it would output whatever you've told it to in the method. In the case of the example above it would be the x.somevalue member.
If the type of the member can't be added directly to an ostream, then you would need to overload the << operator for that type also, using the same method as above.
it's very easy, just implement :
std::ostream & operator<<(std::ostream & os, const myclass & foo)
{
os << foo.var;
return os;
}
You need to return a reference to os in order to chain the outpout (cout << foo << 42 << endl)
Even though other answer provide correct code, it is also recommended to use a hidden friend function to implement the operator<<. Hidden friend functions has a more limited scope, therefore results in a faster compilation. Since there is less overloads cluttering the namespace scope, the compiler has less lookup to do.
struct myclass {
int i;
friend auto operator<<(std::ostream& os, myclass const& m) -> std::ostream& {
return os << m.i;
}
};
int main() {
auto const x = myclass{10};
std::cout << x;
return 0;
}
Alternative:
struct myclass {
int i;
inline operator int() const
{
return i;
}
};
UPD: I can see now how stupid this question is, it's just my misunderstanding of C++ constructions.
I got stuck with operator assignment problem - it doesn't work as expected. Here's the example code:
#include <iostream>
class TestClass{
private:
int pop;
public:
TestClass(){
std::cout<<"Default Constuctor\n";
}
TestClass(int i):pop(i){
std::cout<<"Param Constuctor\n";
}
TestClass &operator=(const TestClass &other){
std::cout<<"Assignment Operator \n";
return *this;
}
friend std::ostream &operator<<(std::ostream &out, TestClass &x);
};
std::ostream &operator<<(std::ostream &out, TestClass &x){
out<<" This is the TestClass with pop=" << x.pop <<"\n";
return out;
}
int main()
{
TestClass P0(333);
TestClass P_foo(555);
P_foo = P0;
std::cout << P0;
std::cout << P_foo;
return 0;
}
The result of this program is
Param Constuctor
Param Constuctor
Assignment Operator
This is the TestClass with pop=333
This is the TestClass with pop=555
So P_foo object preserves the initialized value 555.
If I comment out my custom assignment operator, the program works as expected.
Param Constuctor
Param Constuctor
This is the TestClass with pop=333
This is the TestClass with pop=333
What's wrong with my assignment operator function?
What's wrong with my assignment operator function?
At no point does *this get modified in your implementation of operator=:
TestClass &operator=(const TestClass &other){
std::cout<<"Assignment Operator \n";
return *this;
}
I see several issues with this code:
pop is not being initialized in the default constructor (which you are not calling, but you should still implement it properly, if you are going to implement it manually at all).
operator= is not updating the value of this->pop at all, which is the root cause of your issue. If you declare operator=, you are responsible for implementing it properly yourself, the compiler will not help you at all. But if you do not declare operator=, the compiler will auto-generate a default implementation that will assign a copy of the pop value for you.
operator<< should be taking the TestClass parameter by const reference.
Try this:
#include <iostream>
class TestClass{
private:
int pop;
public:
TestClass() : pop(0) { // <-- add this value!
std::cout << "Default Constructor\n";
}
TestClass(int i) : pop(i) {
std::cout << "Param Constructor\n";
}
TestClass& operator=(const TestClass &other){
std::cout << "Assignment Operator\n";
pop = other.pop; // <-- add this!
return *this;
}
friend std::ostream& operator<<(std::ostream &out, const TestClass &x);
};
std::ostream& operator<<(std::ostream &out, const TestClass &x){
out << " This is the TestClass with pop=" << x.pop << "\n";
return out;
}
int main()
{
TestClass P0(333);
TestClass P_foo(555);
P_foo = P0; // <-- this will work as expected now
std::cout << P0;
std::cout << P_foo;
return 0;
}
TestClass &operator=(const TestClass &other){
std::cout << "Assignment Operator \n";
pop = other.pop; // You need to add this
return *this;
}
The issue is that the assignment operator you defined doesn't assign anything to the instance it is called on.
Read through your operator again:
TestClass &operator=(const TestClass &other){
std::cout<<"Assignment Operator \n";
return *this;
}
You can see that two things happen. Assignment Operator is printed, and *this is returned. But your "other" TestClass is not used at all, and the data in this is never modified.
You can fix that by assigning to this->pop like so:
TestClass &operator=(const TestClass &other){
std::cout<<"Assignment Operator \n";
pop = other.pop; // Now this->pop will be assigned a new value from other.pop
return *this;
}
Now when you assign P_foo = P0, P_foo is successfully assigned the pop value from P0.
I am learning C++, and learned that int-types are just premade classes. So I thought maybe i should try to create one.
What I want to do basically is a
normal class of int
int x;
x=7;
cout << x;
// Output is 7 on screen.
so similarly...
abc x;
x=7;
cout << x;
What would I put in
class abc{
\\ HERE!!!!!!
};
so I could do this
class SomeClass {
public:
int x;
SomeClass(int x) {
this->x = x;
}
};
int main(int argc, char *argv[]) {
SomeClass s = 5;
cout << s.x << "\n"; // 5
s = 17;
cout << s.x << "\n"; // 17
return 0;
}
But as you can see I have to use s.x to print the value - I just want to use 's'.
I am doing it as an experiment, I don't want to hear about how this method is good or bad, pointless or revolutionary, or can 't be done. I remember once I did it. But only by copying and pasting code that I didn't fully understand, and have even forgotten about.
and learned that int, types, are just premade classes
This is completely false. Still, you have complete control on how your class will behave in expressions, since you can overload (almost) any operator. What you are missing here is the usual operator<< overload that is invoked when you do:
cout<<s;
You can create it like this:
std::ostream & operator<<(std::ostream & os, const SomeClass & Right)
{
Os<<Right.x;
return Os;
}
For more information, see the FAQ about operator overloading.
the << and >> are basically function names. you need to define them for your class. same with the +, -, * and all the other operators. here is how:
http://courses.cms.caltech.edu/cs11/material/cpp/donnie/cpp-ops.html
You need to overload operator<< for your class, like so:
class abc
{
public:
abc(int x) : m_X(x) {}
private:
int m_X;
friend std::ostream& operator<<(std::ostream& stream, const abc& obj);
};
std::ostream& operator<<(std::ostream& os, const abc& obj)
{
return os << obj.m_X;
}
You don't have to friend your operator<< overload unless you want to access protected/private members.
You must define in your class abc cast operator to int and assignment operator from int, like in this template class:
template <class T>
class TypeWrapper {
public:
TypeWrapper(const T& value) : value(value) {}
TypeWrapper() {}
operator T() const { return value; }
TypeWrapper& operator (const T& value) { this->value = value; return *this; }
private:
T value;
};
int main() {
TypeWrapper<int> x;
x = 7;
cout << x << endl;
}
You want to overload the output operator:
std::ostream& operator<< (std::ostream& out, SomeClass const& value) {
// format value appropriately
return out;
}