Thread: Passing an Abstract class to an abstract Class member function

  1. #1
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275

    Passing an Abstract class to an abstract Class member function

    Hi

    I have an abstract class whose some of member functions accepts parameters of type as the same abstract class.
    Code:
    class virtualC {
    public:
      virtual void function1() = 0;
      virtual string function2(virtualC &k) = 0;
    };
    My question is, why do I have to pass a reference instead of object itself?

    Thanks in advance

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by fnoyan View Post
    My question is, why do I have to pass a reference instead of object itself?
    So that the function can take references to objects of the derived classes too (without splicing).
    Also, copying a big fat object isn't something you'd want to do often.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    To pass an "object itself" (more accurately, pass by value) it is necessary to create an temporary virtualC that is a copy of the object passed. Creating an instance of an abstract class is not permitted.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    Quote Originally Posted by grumpy View Post
    To pass an "object itself" (more accurately, pass by value) it is necessary to create an temporary virtualC that is a copy of the object passed. Creating an instance of an abstract class is not permitted.
    Well, then this should also work
    Code:
    virtual string function2(virtualC *k) = 0;

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it would work, but why would you do it? Why not use 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-04-2011, 12:40 AM
  2. Something like abstract class
    By Liakos in forum C Programming
    Replies: 1
    Last Post: 03-23-2011, 04:34 AM
  3. abstract class member cannot be overladed?
    By dudeomanodude in forum C++ Programming
    Replies: 10
    Last Post: 12-10-2008, 11:12 AM
  4. abstract class
    By xddxogm3 in forum C++ Programming
    Replies: 5
    Last Post: 01-01-2005, 09:08 AM
  5. abstract vs pure abstract class
    By Kohatian 3279 in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2002, 11:12 AM