Passing an Abstract class to an abstract Class member function

This is a discussion on Passing an Abstract class to an abstract Class member function within the C++ Programming forums, part of the General Programming Boards category; Hi I have an abstract class whose some of member functions accepts parameters of type as the same abstract class. ...

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

    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    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.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,656
    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, Sunshine, 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
    269
    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
    Posts
    23,020
    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, 01:40 AM
  2. Something like abstract class
    By Liakos in forum C Programming
    Replies: 1
    Last Post: 03-23-2011, 05: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, 12:12 PM

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