yes i'm very sure.
my original design had a pointer. and he said "remember when we talked about always using smart pointers?", to which i replied are there no exceptions allowed? and he said no.
@matsp
thanks! that works.
@matsp
btw, it compiles, but there's an infinite loop because the assignment operator needs the copy constructor and vice versa.
bah! i'll just remove the const for now.
Christ... I know Elysia can be a pain about arguing every little thing in someone's code, gramar, spelling, or even facts they present, but just take her advice. She isn't trying to sabotage your project with her mysterious smart pointers. Besides, the newer standard of C++ will heavily use them. So it doesn't hurt to get in some practice now (though who knows when compilers will actually conform to the standard...)
i want to avoid smart pointers for now because i don't want to use the heap.
You are being quite frustrating about this... The const thing isn't bad. Given what you are attempting to accomplish, the parameter should be const.
I don't see much other choice. Either a raw pointer or a smart pointer or you'll have to rethink your whole design, because references can't be reassigned. It's not a problem with const. It's a language restriction (or rather, you're trying to use the wrong tool for the problem).
the alternate solution is to get rid of the const reference and just have it make copies, which is what i wanted to avoid in the first place...
Which you should be avoiding in the first place. That will throw off your reference counters, correct?
I did notice that the operator= is recursing forever.
Actually, as long as you don't assing Foo objects, but only need to construct new foo objects from existing ones, you can do this:
[complete example]
--Code:#include <iostream> class Bar { public: Bar(int ax = 0) : x(ax) {} void print(void) const { std::cout << x << std::endl; } private: int x; }; class Foo { public: Foo(const Bar& bar); Foo(const Foo& foo); void Print() { m_bar.print(); } private: const Bar& m_bar; }; Foo::Foo(const Bar& bar) : m_bar(bar) { } Foo::Foo(const Foo &foo) : m_bar(foo.m_bar) { } int main() { Bar bar(42), baz(47); Foo a(bar); Foo b(baz); Foo c(b); a.Print(); c.Print(); return 0; }
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
O_o
This is stupid. Pointers have their place...
Even if you can't use vanilla pointer and don't want to use smart pointers, you could still use "do nothing" wrappers.
Show your boss the code below and explain, with small words, that when stupid requirements are forced coders must make bad decisions in order to fulfill the requirements.
Soma
Code:#include <iostream> struct ugly_crap_helper1 { ugly_crap_helper1 ( const int & r_f ): r_m(r_f) { } const int & r_m; }; struct ugly_crap_helper2 { ugly_crap_helper2 ( const int * p_f ): p_m(p_f) { } const int * p_m; }; union ugly_crap { ugly_crap_helper1 * h1_m; ugly_crap_helper2 * h2_m; }; struct test { test ( const int & i_f ): h1_m(i_f) { } test ( const test & f ): h1_m(f.h1_m.r_m) { } test & operator = ( const test & rhs_f ) { ugly_crap crap; crap.h1_m = &h1_m; crap.h2_m->p_m = &rhs_f.h1_m.r_m; return(*this); } ugly_crap_helper1 h1_m; }; void print ( const int & i1_f, const int & i2_f, const test & t1_f, const test & t2_f ) { std::cout << "i1: (" << i1_f << ") " << "i2: (" << i2_f << ") " << "t1: (" << t1_f.h1_m.r_m << ") " << "t2: (" << t2_f.h1_m.r_m << ")\n" ; } int main() { int i1(1); int i2(2); test t1(i1); test t2(i2); print(i1, i2, t1, t2); i1 = 3; i2 = 4; print(i1, i2, t1, t2); t2 = t1; print(i1, i2, t1, t2); return(0); }
ok, 1 final question:
when the assignment operator is called, 'a' will fall off the stack and its new value will be 'tmp' right?Code:Foo operator = (const Foo& foo) { Foo tmp(foo.bar); return tmp; } int main() { Foo a; Foo b; a = b; }
Hehe, you should complain to your boss: "Look, I'm a programmer, I'm doing the code, so I know best what's good and not good. Please allow me to do my work properly instead of slowing me down and force me to compromise and create complex solutions where simple ones will do by forcing restrictions you know nothing about down on me."
Oh and phantomotap, I love your examples. They're always so complex ^_^
If that code were presented to the boss, I'm sure the boss would be scratching his/her head and wondering WTF it does ^_^