Class Inheritance

This is a discussion on Class Inheritance within the C++ Programming forums, part of the General Programming Boards category; This question is going to sound absolutely rediculous especially since I've already tried to look up on Google without much ...

  1. #1
    Professional Chef leeor_net's Avatar
    Join Date
    Apr 2004
    Location
    Charles Town, WV
    Posts
    147

    Class Inheritance

    This question is going to sound absolutely rediculous especially since I've already tried to look up on Google without much success... worst part is, I know it to I just haven't worked in C++ for awhile and I know I'm going to bang my head on my desk when I get an answer... so here goes:

    I'm creating a base class from which there will be derived classes. Whoop dee doo. However, I can't seem to figure out how the constructors work with inherited classes:

    Here's the code (minus comments for the sake of simplicity):

    Code:
    class Resource
    {
    public:
    	Resource(char *filePath);
    	~Resource();
    
    	char *getName();
    	virtual bool load() = 0;
    
    protected:
    	char *mResourceName;
    };
    
    class Image : public Resource
    {
    public:
    	bool load();
    private:
    };
    Now, here's where I have the problem:

    Code:
    Image *img = new Image("filename.ext");
    Generates the following error:

    Code:
    error C2664: 'Image::Image' : cannot convert parameter 1 from 'const char [9]' to 'const Image &'
    Clearly I forgot something... but I just can't remember what.

    I really do feel foolish about this but I hope that at the very least it's good for a couple of laughs.

    Thanks for your time.

    EDIT:
    Just resolved my problem. Thanks for any would-be help!
    Last edited by leeor_net; 09-23-2008 at 07:31 PM. Reason: Resolved
    - Leeor

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    What was the solution?

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Constructors aren't inherited in the sense that they become constructors for the derived class and can be used to construct the object.
    You would need a custom constructor for Image that takes const char* and passes it on to the base class.
    I assume (or hope) that your solution is similar to this?
    And btw, you do know you can write char* var and char * var instead of char *var?
    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. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Code:
    Resource(char *filePath);
    Should be const char*. You don't intend to modify that string, to 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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-02-2005, 01:27 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Inheritance, istream, class datamember
    By guda in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2004, 01:25 PM
  4. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 01:46 PM
  5. Need some help on class inheritance
    By HelpMe in forum C++ Programming
    Replies: 1
    Last Post: 05-21-2002, 04:44 PM

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