-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated operator= such that the basic data type is considered
- Loading branch information
1 parent
ef1beef
commit 01447cf
Showing
5 changed files
with
130 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include <iostream> | ||
#include <ostream> | ||
|
||
class Foo { | ||
public: | ||
bool InUsage = false; | ||
int attr = 0; | ||
|
||
Foo(int i) : attr(i){}; | ||
Foo() : attr(0){}; | ||
|
||
template <typename T> Foo &operator=(const T &&otherFoo) { | ||
if (InUsage) { | ||
std::cout << "test" << std::endl; | ||
int temp = otherFoo[0]; | ||
attr = temp; | ||
} else { | ||
attr = otherFoo[0]; | ||
} | ||
return *this; | ||
} | ||
|
||
friend std::ostream &operator<<(std::ostream &os, const Foo &f) { | ||
os << f.attr << std::endl; | ||
return os; | ||
} | ||
}; | ||
|
||
template <typename T> class Bar { | ||
public: | ||
const T &obj; | ||
|
||
Bar(T &&obj_) : obj(obj_) { obj_.InUsage = true; }; | ||
Bar(T &obj_) : obj(obj_) { obj_.InUsage = true; }; | ||
|
||
int operator[](size_t i) { return obj.attr + 1 + obj.attr; } | ||
}; | ||
|
||
int main() { | ||
Foo f1; | ||
Bar b(f1); | ||
Foo f2(3); | ||
f1 = f2; | ||
std::cout << f1 << std::endl; | ||
return 0; | ||
} |