Thread: copy constructor for a class with a const reference member...

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by bling View Post
    - but i DO want to be able to redirect it to something else, which i'm guess the const and no-reseating for references is messing me up...
    Impossible with references. Set once. Forget. Can't be changed.
    You seem to need a smart pointer to const T. Can be reassigned, but you can't change the data. Plus it will keep ref count so no need to manage memory and no leaks.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    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.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And mats solution seems more like a nasty hack that will cause you headaches sooner or later.
    Just be warned if you're going to use it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    @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.

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by bling View Post
    bah! i'll just remove the const for now.
    It still won't work because you can't reassign a reference.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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...)

  7. #22
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    i want to avoid smart pointers for now because i don't want to use the heap.

  8. #23
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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.

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    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...

  11. #26
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Which you should be avoiding in the first place. That will throw off your reference counters, correct?

  12. #27
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  13. #28
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    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);
    }

  14. #29
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    ok, 1 final question:

    Code:
    Foo operator = (const Foo& foo) {
      Foo tmp(foo.bar);
      return tmp;
    }
    
    int main() {
      Foo a;
      Foo b;
      a = b;
    }
    when the assignment operator is called, 'a' will fall off the stack and its new value will be 'tmp' right?

  15. #30
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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 ^_^
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Immediate programming help! Please!
    By xMEGANx in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2008, 12:52 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM