Thread: Can you assign an object of a derived class to an object of its base class?

  1. #1
    Unregistered
    Guest

    Can you assign an object of a derived class to an object of its base class?

    without any class being modified to accept those parameters?

  2. #2
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    I believe you can. Try it. I know for sure that you can pass a derived class to a function where the parameter expects a base class, so why not?
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  3. #3
    GeekSeb
    Guest
    I DOnt Think So.

    Im assuming you are using protected variables. If it is a common varaible to both the base class and the derived class then yeah it should probably work. Otherwise, trying to assign a derived class variable to a non-existant variable in a base class would be invalid.

  4. #4
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    That's what I meant. I've never tried it, but it seems logical that you could do it. C++ still manages to surprise me with its flexibility sometimes.
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. derived class can not access base class protected member?
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2007, 06:32 PM
  2. derived class
    By BKurosawa in forum C++ Programming
    Replies: 9
    Last Post: 08-09-2007, 02:18 PM
  3. Base class initialization
    By VirtualAce in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2004, 04:52 AM
  4. Replies: 1
    Last Post: 11-27-2001, 01:07 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM