By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Dec 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

Dec 21 '05 #2

P: n/a
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 [i] overload the
private same name function?" Try qualifying your call to initialize:

Cdirinfo::initialize(path)

Cheers! --M

Dec 21 '05 #3

P: n/a

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.

Dec 21 '05 #4

P: n/a
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!

Dec 21 '05 #5

P: n/a
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.
Dec 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.