why default assignment can NOT be used for const reference?

This is a discussion on why default assignment can NOT be used for const reference? within the C++ Programming forums, part of the General Programming Boards category; Who is "you"?...

  1. #16
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Who is "you"?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  2. #17
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by CornedBee View Post
    Who is "you"?
    The poster above, Elysia. Sorry for the confusion.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #18
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Quote Originally Posted by anon View Post
    As I understand it, instances of an abstract base class do exist - but only as a part of a derived class. You make it sound as if the abstract part of an instance was absolutely nothing.
    The whole point of abstract is that they can only be derived and not be created, of course, but that doesn't mean it seems logical to make a pure virtual destructor since pure virtual functions aren't meant to be called. So far as I understand them, they only make a class abstract and thus needs to overriden within a derived class.

  4. #19
    The larch
    Join Date
    May 2006
    Posts
    3,573
    So far as I understand them, they only make a class abstract and thus needs to overriden within a derived class.
    But it seems to me that this doesn't mean that you cannot provide an implementation for pure virtual methods and call it explicitly (from concrete derived classes) or implicitly (the destructor).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #20
    Registered User
    Join Date
    Jun 2005
    Posts
    6,245
    Quote Originally Posted by Elysia View Post
    The whole point of abstract is that they can only be derived and not be created, of course, but that doesn't mean it seems logical to make a pure virtual destructor since pure virtual functions aren't meant to be called. So far as I understand them, they only make a class abstract and thus needs to overriden within a derived class.
    Not really. The primary purpose of a pure virtual function is to make the class abstract (i.e. the class represents an abstraction, and instances of the class cannot be created). For pure virtual functions other than the destructor, derived classes must override any inherited virtual functions or they will remain abstract. Pure virtual destructors serve to ensure that the class cannot be instantiated, but destructors are special in the sense that ALL classes must have a defined (i.e. implemented) destructor and, if the programmer has declared a destructor, programmer must also define it. For example;
    Code:
    class Base
    {
        public:
           virtual ~Base() = 0;   // declaration of destructor
           // whatever
    };
    
    Base::~Base()   // definition of this destructor is required despite it being pure virtual
    {}
    
    class Derived : public Base
    {
        public:
             ~Derived();
    };
    
    Derived::~Derived()
    {}
    
    int main()
    {
        Derived d;
    }
    In practice, if Base::~Base() is not defined in the above, then the most common result is a linker error. The reason is that, when a Derived is destroyed, that Derived::~Derived is invoked and then Base::~Base() is invoked. However, Derived::~Derived() does not override Base::~Base() as both functions must exist

    Coming back to the original question, default assignment can be used for const reference arguments, even for pure virtual functions. The requirements are that the default value has const attributes (eg it is a compile time constant), and that a conversion (eg conversion constructor) exists between the value given and the type of the argument. That's the theory: there is also practice -- such approaches are not a good idea with virtual functions, because derived classes may provide different default values than base classes, and therefore the result may not be what the programmer expects in some cases. Hence the value of techniques such the NVI pattern mentioned by CornedBee.
    Last edited by grumpy; 11-29-2007 at 06:34 AM.

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Quote Originally Posted by grumpy View Post
    Not really. The primary purpose of a pure virtual function is to make the class abstract (i.e. the class represents an abstraction, and instances of the class cannot be created). For pure virtual functions other than the destructor, derived classes must override any inherited virtual functions or they will remain abstract. Pure virtual destructors serve to ensure that the class cannot be instantiated, but destructors are special in the sense that ALL classes must have a defined (i.e. implemented) destructor and, if the programmer has declared a destructor, programmer must also define it. For example;
    Yes, I know what they are and everything, and it's exactly as I described. If were create pure virtual functions to make sure a class can't be instaciated, then what's the point of defining them when they're always overriden by derived classes? I understand the NEED to do so, but I do not see a need to make a pure virutal destructor since, as you mentioned, they must be defined, but since it's a pure virtual function, it isn't supposed to be called, and thus it doesn't meet the define for my interpretation of a pure virtual function.
    In short, it can be a pure virtual function, but that defeats the purpose of a pure virtual function in my book, or at least seems illogical or bends the rules as to why pure virtual functions were made an addition to the language.
    Last edited by Elysia; 11-29-2007 at 06:37 AM.

  7. #22
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I don't like to look at pure virtual functions meaning that they aren't supposed to be called.

    I think a better description is that a pure virtual function means that the function must be overridden.

    If you look at it that way then the pure virtual destructor and any other pure virtual function with an implementation makes sense. You are forcing classes that concretely implement your abstract interface to implement that function.

  8. #23
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Quote Originally Posted by Daved View Post
    I think a better description is that a pure virtual function means that the function must be overridden.
    Indeed, and then what's the purpose of the pure virtual function if it's overriden and isn't called?
    But your reply made me think of something. It might still be possible to use the base class's function as a base - base code that is the same for all derived classes, and since it's a base class not suppoed to be instanciated, it can be declared a pure virtual, and the derived classes override, do some work and call the base class's function. I suppose that might be a valid idea.

  9. #24
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I haven't used it myself, but I guess one motivation for making a function pure virtual and still providing an implementation (as default behaviour for the hierarchy) is to ensure that the writer of the derived class explicitly states that he want's to use the default behaviour (and not get the default behaviour accidentally because he forgot to override this method).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #25
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Quote Originally Posted by anon View Post
    ...he...
    they/he/she/(it?)

    ...(and not get the default behaviour accidentally because he forgot to override this method).
    Eh? If someone forgets to override the function, you'll get an abstract derived class and the code won't compile.

  11. #26
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Eh? If someone forgets to override the function, you'll get an abstract derived class and the code won't compile.
    I think anon was contrasting default implementations with pure virtual functions versus default implementations with virtual functions.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #27
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by Elysia View Post
    they/he/she/(it?)
    Sorry if it bothers you. I guess languages reflect the chauvinist history of mankind and I even try to use the neutral singular "they" sometimes, but I guess it would be easier to accept that

    he ... 2. used to talk about anyone, everyone, or an unknown person who may be either male or female
    (Longman)
    and stop raping the language

    Eh? If someone forgets to override the function, you'll get an abstract derived class and the code won't compile.
    Yes, but you may want to provide a default implementation that has to be called explicitly.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  13. #28
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I've given example code with an implementation of a pure virtual function, have I not?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #29
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Yes, but I think Elysia was still confused about the usefulness.

  15. #30
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Well, having a default implementation even of pure virtual functions is useful if there's stuff that most overriding functions have to do. (But not if all overriding functions have to do it - to enforce this, use the NVI pattern.)

    The destructor thing is, admittedly, something of a hack. It's like a Java class with the abstract modifier but no abstract functions. You can't instantiate the class itself, but you can instantiate a subclass, even if it did nothing but derive.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Utilizing another compiled program for a task.
    By kotoroshinoto in forum C Programming
    Replies: 6
    Last Post: 06-03-2008, 01:43 PM
  2. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  3. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM
  5. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21