Thread: Class Inheritance

  1. #1
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    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 06:31 PM. Reason: Resolved

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

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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,895
    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, 12:27 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Inheritance, istream, class datamember
    By guda in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2004, 12:25 PM
  4. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  5. Need some help on class inheritance
    By HelpMe in forum C++ Programming
    Replies: 1
    Last Post: 05-21-2002, 03:44 PM