The problem of inheritance? Why can't it overload the private same name function ?Help?

Discussion in 'C++' started by dragon, Dec 21, 2005.

  1. dragon

    dragon Guest

    error!
    33 E:\program\wukexin\file\file.h `bool My_lib::Cfileinfo::initialize(const
    char*)' is private
    19 E:\program\wukexin\dir\dir.cpp within this context
    ////////////////////////////////////
    #ifndef fileH
    #define fileH
    ////
    #include "windows.h"
    namespace My_lib{
    class Cfileinfo{
    public:
    Cfileinfo(const std::string& name);
    Cfileinfo(const char* name);
    virtual ~Cfileinfo();
    virtual void display();
    virtual unsigned long size() const;
    protected:
    HANDLE m_hfile;
    WIN32_FIND_DATA m_wfd;
    private:
    Cfileinfo(const Cfileinfo& X);
    Cfileinfo operator= (const Cfileinfo& X);
    bool initialize(const char* name);// linenumber 33
    bool clean();
    };
    }//end namespace My_lib

    #endif //fileH
    /*******************************
    #ifndef dirH
    #define dirH
    //
    #include "file\file.h"
    namespace My_lib{
    class Cdirinfo:public Cfileinfo{
    public:
    Cdirinfo(const char* path);
    Cdirinfo(const std::string& path);
    ~Cdirinfo();
    void display();
    private:
    std::list<std::string> m_record;
    bool intialize(const char* name);
    void traverse();
    void scan(const char* path);
    Cdirinfo(const Cdirinfo& X);
    Cdirinfo operator= (const Cdirinfo& X);
    };
    }
    #endif// dirH
    /*****************************
    namespace My_lib{
    Cdirinfo::Cdirinfo(const char* path):Cfileinfo(path){
    //#ifdef DEBUG
    Ctracer tracer( std::string("Cdirinfo"),std::string(path) );
    //#endif //debug
    if( false==initialize(path) ){ //linenumber 19
    cout<<"Cdirinfo::initialize("<<path<<") run failed!\n";
    throw;
    }
    }
    }//end
     
    dragon, Dec 21, 2005
    #1
    1. Advertising

  2. dragon

    Sumanth Guest

    Re: The problem of inheritance? Why can't it overload the private same name function ?Help?

    Hi,
    If you think CfileInfo::Initialize will be useful to its child classes
    like in Cdirinfo, then make the method CfileInfo::Initialize protected.
    A private method cant be accessed by its child classes because its
    "private".

    And you cant overload because the private Initialize method does exist
    in Cdirinfo, its just that you are not allowed to access it.

    Regards,
    Sumanth
     
    Sumanth, Dec 21, 2005
    #2
    1. Advertising

  3. dragon

    mlimber Guest

    Re: The problem of inheritance? Why can't it overload the private same name function ?Help?

    dragon wrote:
    > error!
    > 33 E:\program\wukexin\file\file.h `bool My_lib::Cfileinfo::initialize(const
    > char*)' is private
    > 19 E:\program\wukexin\dir\dir.cpp within this context
    > ////////////////////////////////////
    > #ifndef fileH
    > #define fileH
    > ////
    > #include "windows.h"
    > namespace My_lib{
    > class Cfileinfo{
    > public:
    > Cfileinfo(const std::string& name);
    > Cfileinfo(const char* name);
    > virtual ~Cfileinfo();
    > virtual void display();
    > virtual unsigned long size() const;
    > protected:
    > HANDLE m_hfile;
    > WIN32_FIND_DATA m_wfd;
    > private:
    > Cfileinfo(const Cfileinfo& X);
    > Cfileinfo operator= (const Cfileinfo& X);
    > bool initialize(const char* name);// linenumber 33
    > bool clean();
    > };
    > }//end namespace My_lib
    >
    > #endif //fileH
    > /*******************************
    > #ifndef dirH
    > #define dirH
    > //
    > #include "file\file.h"
    > namespace My_lib{
    > class Cdirinfo:public Cfileinfo{
    > public:
    > Cdirinfo(const char* path);
    > Cdirinfo(const std::string& path);
    > ~Cdirinfo();
    > void display();
    > private:
    > std::list<std::string> m_record;
    > bool intialize(const char* name);
    > void traverse();
    > void scan(const char* path);
    > Cdirinfo(const Cdirinfo& X);
    > Cdirinfo operator= (const Cdirinfo& X);
    > };
    > }
    > #endif// dirH
    > /*****************************
    > namespace My_lib{
    > Cdirinfo::Cdirinfo(const char* path):Cfileinfo(path){
    > //#ifdef DEBUG
    > Ctracer tracer( std::string("Cdirinfo"),std::string(path) );
    > //#endif //debug
    > if( false==initialize(path) ){ //linenumber 19
    > cout<<"Cdirinfo::initialize("<<path<<") run failed!\n";
    > throw;
    > }
    > }
    > }//end


    Your question from the subject line is "Why can't overload the
    private same name function?" Try qualifying your call to initialize:

    Cdirinfo::initialize(path)

    Cheers! --M
     
    mlimber, Dec 21, 2005
    #3
  4. dragon

    Earl Purple Guest

    Re: The problem of inheritance? Why can't it overload the private same name function ?Help?

    dragon wrote:
    > #include "file\file.h"
    > namespace My_lib{
    > class Cdirinfo:public Cfileinfo{
    > public:
    > Cdirinfo(const char* path);
    > Cdirinfo(const std::string& path);
    > ~Cdirinfo();
    > void display();
    > private:
    > std::list<std::string> m_record;
    > bool intialize(const char* name);
    > void traverse();
    > void scan(const char* path);
    > Cdirinfo(const Cdirinfo& X);
    > Cdirinfo operator= (const Cdirinfo& X);
    > };
    > }
    > #endif// dirH
    > /*****************************


    If you actually copied this in from your code then your problem is a
    typo. In Cdirinfo the function is intialize(). No reason why a compiler
    should complain here, it's a perfectly legitimate name for a function.
     
    Earl Purple, Dec 21, 2005
    #4
  5. dragon

    mlimber Guest

    Re: The problem of inheritance? Why can't it overload the private same name function ?Help?

    Earl Purple wrote:
    > dragon wrote:
    > > #include "file\file.h"
    > > namespace My_lib{
    > > class Cdirinfo:public Cfileinfo{
    > > public:
    > > Cdirinfo(const char* path);
    > > Cdirinfo(const std::string& path);
    > > ~Cdirinfo();
    > > void display();
    > > private:
    > > std::list<std::string> m_record;
    > > bool intialize(const char* name);
    > > void traverse();
    > > void scan(const char* path);
    > > Cdirinfo(const Cdirinfo& X);
    > > Cdirinfo operator= (const Cdirinfo& X);
    > > };
    > > }
    > > #endif// dirH
    > > /*****************************

    >
    > If you actually copied this in from your code then your problem is a
    > typo. In Cdirinfo the function is intialize(). No reason why a compiler
    > should complain here, it's a perfectly legitimate name for a function.


    Good catch!
     
    mlimber, Dec 21, 2005
    #5
  6. dragon

    dragon Guest

    Re: The problem of inheritance? Why can't it overload the private same name function ?Help?


    > If you actually copied this in from your code then your problem is a
    > typo. In Cdirinfo the function is intialize(). No reason why a compiler
    > should complain here, it's a perfectly legitimate name for a function.


    Thank you very much. I type my function error!. But I have learned that it
    "never redefine an inherited nonvirtual function" from effective C++, Why?
    If I overload the private same function, through compiler( gcc 3.4.2), why
    not I redefined an inherited "private" novirtual function. Could you tell
    me. I lookup many books, but
    I have not found my answer.
     
    dragon, Dec 22, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Piotre Ugrumov
    Replies:
    3
    Views:
    384
    Nick Hounsome
    Jan 25, 2004
  2. qazmlp
    Replies:
    19
    Views:
    818
    Daniel T.
    Feb 4, 2004
  3. Jeremy Bowers
    Replies:
    3
    Views:
    545
    Steven Bethard
    Jan 24, 2005
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,138
    Smokey Grindel
    Dec 2, 2006
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    274
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page