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

D

dragon

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
 
S

Sumanth

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
 
M

mlimber

dragon said:
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
 
E

Earl Purple

dragon said:
#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.
 
M

mlimber

Earl said:
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!
 
D

dragon

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top