-
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.
Fixed chaining_main.cpp after debugging
- Loading branch information
1 parent
c678ab6
commit 64d84ac
Showing
1 changed file
with
29 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,63 @@ | ||
#include <iostream.h> | ||
#include <string.h> | ||
#include <iostream> | ||
#include <string> | ||
#include <cassert> | ||
using namespace std; | ||
|
||
class Container | ||
{ | ||
union | ||
{ | ||
string str; | ||
int val; | ||
}; | ||
bool is_val; | ||
private: | ||
string str; | ||
int val; | ||
bool is_string; | ||
|
||
public: | ||
Container(string str); | ||
Container(int val); | ||
|
||
Container chain(int val); | ||
operator string(); | ||
}; | ||
|
||
Container::Container(string str) : str(str), is_val(false) | ||
Container::Container(string str) : str(str), is_string(true) | ||
{ | ||
|
||
} | ||
Container::Container(int val) : val(val), is_val(true) | ||
|
||
Container::Container(int val) : val(val), is_string(false) | ||
{ | ||
|
||
} | ||
|
||
Container Container:chain(int val) | ||
Container Container::chain(int val) | ||
{ | ||
assert(!is_string); | ||
if(this->val > val) | ||
{ | ||
return Container("Hello"); | ||
} | ||
else | ||
{ | ||
return Container(val+1); | ||
} | ||
} | ||
|
||
Container::operator string() | ||
{ | ||
assert(is_string); | ||
return str; | ||
} | ||
|
||
Container chain(int val) | ||
{ | ||
return Container(val); | ||
} | ||
|
||
void foo(string str) | ||
{ | ||
cout<<str<<endl; | ||
} | ||
|
||
int main() | ||
{ | ||
cout<<chain(2).chain(1)<<endl; | ||
foo(chain(2).chain(1)); | ||
} |