Hi All,
I tried something with the C++ I know and some things just seem
strange.
consider:
#include <iostream>
using namespace std;
class base
{
public:
base();
void some_func_2();
private:
void some_func();
};
///////////////////////////
base::base()
{
cout<<"base constructor"<<e ndl;
}
///////////////////////////
void base::some_func ()
{
cout<<"base some func"<<endl;
}
///////////////////////////
void base::some_func _2()
{
cout<<"base some_func_2"<<e ndl;
}
///////////////////////////
class derived: public base
{
public:
void some_func();
static void some_func_2();
};
void derived::some_f unc()
{
cout<<"derived some func"<<endl;
}
///////////////////////////
void derived::some_f unc_2()
{
cout<<"derived some_func_2"<<e ndl;
}
///////////////////////////
void main()
{
base b;
b.some_func_2() ;
base::base();
derived d;
d.some_func();
derived::some_f unc_2();
d.some_func_2() ;
derived *pd;
pd = reinterpret_cas t<derived*>(ne w base());
// derived *pd;
// pd = new base(); /// This will cause errors if
uncommented.
}
Here's the output:
base constructor
base some_func_2
base constructor
base constructor
derived some func
derived some_func_2
derived some_func_2
base constructor
My problems here are that
1. I understood that derived class cannot increase the scope of the
base class methods. In this case 'derived' increased the scope of the
base class private method 'some_func' by making it public and the scope
of base class 'some_func_2' by making it static. Is my understading
incorrect or there's something wrong?
2. The constructor for the base class can be called explicitly, even
though I cannot do anything more with that. i fail to understand why
should it be allowed. Are there any special circumstances where this is
used?
3. Base class handle can take derived class objects but not the other
way around. Casting will remove this error but why doesn't implicit
cast like the previous one work?
I was using Microsoft Visual Studio 6.0.
Thanks in advance,
Regards,
-- Taran 6 2484
"Taran" <ta************ @gmail.com> skrev i meddelandet
news:11******** **************@ u72g2000cwu.goo glegroups.com.. . Hi All,
I tried something with the C++ I know and some things just seem strange.
consider:
#include <iostream>
using namespace std;
class base { public:
base(); void some_func_2(); private: void some_func(); };
/////////////////////////// base::base() { cout<<"base constructor"<<e ndl; } ///////////////////////////
void base::some_func () { cout<<"base some func"<<endl; } ///////////////////////////
void base::some_func _2() { cout<<"base some_func_2"<<e ndl; } ///////////////////////////
class derived: public base { public: void some_func(); static void some_func_2();
};
void derived::some_f unc() { cout<<"derived some func"<<endl; } ///////////////////////////
void derived::some_f unc_2() { cout<<"derived some_func_2"<<e ndl; } ///////////////////////////
void main() { base b; b.some_func_2() ; base::base(); derived d; d.some_func(); derived::some_f unc_2(); d.some_func_2() ;
derived *pd; pd = reinterpret_cas t<derived*>(ne w base());
// derived *pd; // pd = new base(); /// This will cause errors if uncommented.
}
Here's the output:
base constructor base some_func_2 base constructor base constructor derived some func derived some_func_2 derived some_func_2 base constructor
My problems here are that
1. I understood that derived class cannot increase the scope of the base class methods. In this case 'derived' increased the scope of the base class private method 'some_func' by making it public and the scope of base class 'some_func_2' by making it static. Is my understading incorrect or there's something wrong?
The functions defined in derived hides the functions in the class
base. It's a another set of functions, with the same names. It doesn't
affect the functions of the base class. 2. The constructor for the base class can be called explicitly, even though I cannot do anything more with that. i fail to understand why should it be allowed. Are there any special circumstances where this is used?
Not very useful, but also not specifically forbidden by the standard.
Why should it be? 3. Base class handle can take derived class objects but not the other way around.
In C++ it is called a pointer, not a handle. A base class pointer can
point to a derived class, because there is a base part in the derived
class. A derived class can act as its base, because it has inherited
(some of) its properties.
Casting will remove this error but why doesn't implicit cast like the previous one work?
Casting doesn't remove the error, it just tells the compiler to shut
up -- "Trust me, I know what I'm doing!".
If that isn't really true, all kinds of nasty things are likely to
happen. :-)
Bo Persson
Taran wrote: Hi All,
I tried something with the C++ I know and some things just seem strange.
consider:
#include <iostream>
using namespace std;
class base { public:
base(); void some_func_2(); private: void some_func(); };
/////////////////////////// base::base() { cout<<"base constructor"<<e ndl; } ///////////////////////////
void base::some_func () { cout<<"base some func"<<endl; } ///////////////////////////
void base::some_func _2() { cout<<"base some_func_2"<<e ndl; } ///////////////////////////
class derived: public base { public: void some_func(); static void some_func_2();
};
void derived::some_f unc() { cout<<"derived some func"<<endl; } ///////////////////////////
void derived::some_f unc_2() { cout<<"derived some_func_2"<<e ndl; } ///////////////////////////
void main() { base b; b.some_func_2() ; base::base(); derived d; d.some_func(); derived::some_f unc_2(); d.some_func_2() ;
derived *pd; pd = reinterpret_cas t<derived*>(ne w base());
// derived *pd; // pd = new base(); /// This will cause errors if uncommented.
}
Here's the output:
base constructor base some_func_2 base constructor base constructor derived some func derived some_func_2 derived some_func_2 base constructor
My problems here are that
1. I understood that derived class cannot increase the scope of the
Not sure what you mean here by "increase the scope"...
base class methods. In this case 'derived' increased the scope of the base class private method 'some_func' by making it public
No, it didn't. 'derived' has its own function 'some_func' that is not
related to the function named the same in the 'base' class.
and the scope of base class 'some_func_2' by making it static. Is my understading incorrect or there's something wrong?
Nothing changes the scope of nothing here. Two new functions introduced
by the 'derived' type. Both hide the functions with the same name from
the 'base' class.
2. The constructor for the base class can be called explicitly, even
No. The syntax you used creates a temporary object (and discards it
promptly). Yes, it causes the constructor to be called, but it doesn't
mean that you can call a construtor.
though I cannot do anything more with that. i fail to understand why should it be allowed. Are there any special circumstances where this is used?
Yes. If you want to create a temporary object and use it.
3. Base class handle
What's that?
can take derived class objects but not the other way around.
You mean, a pointer to a derived class can be converted to a pointer
of a base class? Yes. A couple conditions apply, but not in this case.
Casting will remove this error but why doesn't implicit cast like the previous one work?
Which error? Which implicit cast? Which one is "previous"?
I was using Microsoft Visual Studio 6.0.
You should get yourself VC++ Express 2005 and enjoy a much better
compiler (although it shouldn't matter here).
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
I think you are a newer,you have a long way to go.
> #include <iostream> using namespace std;
class base { public:
base(); void some_func_2(); private: void some_func(); };
/////////////////////////// base::base() { cout<<"base constructor"<<e ndl; } ///////////////////////////
void base::some_func () { cout<<"base some func"<<endl; } ///////////////////////////
void base::some_func _2() { cout<<"base some_func_2"<<e ndl; } ///////////////////////////
class derived: public base { public: void some_func(); static void some_func_2();
};
void derived::some_f unc() { cout<<"derived some func"<<endl; } ///////////////////////////
void derived::some_f unc_2() { cout<<"derived some_func_2"<<e ndl; } ///////////////////////////
void main() { base b; b.some_func_2() ; base::base(); derived d; d.some_func(); derived::some_f unc_2(); d.some_func_2() ;
derived *pd; pd = reinterpret_cas t<derived*>(ne w base());
// derived *pd; // pd = new base(); /// This will cause errors if uncommented.
Of course it is an error. Don't treat a fruit as an apple, because it
may not be. Treat an apple as a fruit, a derived as a base, not the
other way round. }
Here's the output:
base constructor base some_func_2 base constructor base constructor derived some func derived some_func_2 derived some_func_2 base constructor
My problems here are that
1. I understood that derived class cannot increase the scope of the base class methods. In this case 'derived' increased the scope of the base class private method 'some_func' by making it public and the scope of base class 'some_func_2' by making it static. Is my understading incorrect or there's something wrong?
The class derive does not and cannot alter the definition of base. Hence
the "scope" of base can neither be "increased" nor "decreased" .
The base class has 3 members:
* the constructor base::base
* base::some_func
* base::some_func _2
The derived class has 5 members:
* the constructor derived::derive d
* static derived::some_f unc_2
* derived::some_f unc
* base::some_func
* base::some_func 2
The derived::some_f unc simply hides base::some_func in class derived. 2. The constructor for the base class can be called explicitly, even though I cannot do anything more with that. i fail to understand why should it be allowed. Are there any special circumstances where this is used?
It cannot. Detail answered in other posts. 3. Base class handle can take derived class objects but not the other way around. Casting will remove this error but why doesn't implicit cast like the previous one work?
If what you mean by "handle" is what we call "pointer".. .
It is true that a pointer to base can take a pointer to derived. But the
one you commented was doing it the other way round, which doesn't work.
base* p = new derived; // OK
dervied* q = new base; // ERROR
derived* r = p; // ERROR, no implicit conversion supplied
derived* s = static_case<der ived*>(p); //OK, explicit conversion
I was using Microsoft Visual Studio 6.0.
Thanks in advance,
Regards, -- Taran
Regards,
Ben
benben wrote in message <44************ **********@news .optusnet.com.a u>... 3. Base class handle can take derived class objects but not the other way around. Casting will remove this error but why doesn't implicit cast like the previous one work? If what you mean by "handle" is what we call "pointer".. .
It is true that a pointer to base can take a pointer to derived. But the one you commented was doing it the other way round, which doesn't work.
base* p = new derived; // OK
OK?? (See what I was going to post (below) and correct me or comment,
please.)
dervied* q = new base; // ERROR
derived* r = p; // ERROR, no implicit conversion supplied derived* s = static_case<der ived*>(p); //OK, explicit conversion
[ was to the OP ]
// -------------------
[ others have answered your questions, so, I'll just nit-pick a bit.]
In your main() you made the comment " // This will cause errors if
uncommented.". Let's be consistant.
// void main()
int main(){
base b;
b.some_func_2() ;
base::base();
// derived d;
// d.some_func();
// derived::some_f unc_2();
// d.some_func_2() ;
// derived *pd;
// pd = reinterpret_cas t<derived*>(ne w base());
// derived *pd;
// pd = new base(); // This will cause errors if uncommented.
// delete pd;
return 0;
}
There! That's better.
Why? Try this:
class base{ public:
base();
// add this:
~base(); // test, then comment this Dtor and....
// virtual ~base(); // uncomment this Dtor
void some_func_2();
private:
void some_func();
};
///////////////////////////
base::~base(){
cout<<" ~base destructor "<<endl;
}
///////////////////////////
// add a destructor (non-virtual, with output) to your 'derived' class also.
// -- rest of your program same --
In your 'toy' program it won't hurt you much, but, in a big project it could
be a big problem.
[ 'it' being the missing 'virtual destructor' in base class. ]
// -------------------
So, am I right, or out in left field somewhere?
--
Bob R
POVrookie
海风 wrote: I think you are a newer,you have a long way to go.
A newer what? And who are you talking to?
Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Sridhar R |
last post by:
Consider the code below,
class Base(object):
pass
class Derived(object):
def __new__(cls, *args, **kwds):
# some_factory returns an instance of Base
# and I have to derive from this instance!
|
by: Dan Perl |
last post by:
There is something with initializing mutable class attributes that I am
struggling with. I'll use an example to explain:
class Father:
attr1=None # this is OK
attr2= # this is wrong
def foo(self, data):
self.attr1=data
self.attr2.append(data)
The initialization of attr1 is obviously OK, all instances of Father
redefine it in the method foo. But the initialization of attr2 is wrong
|
by: Christian Engström |
last post by:
When I compile the below program with Microsoft Visual C++ version 6, I
get the results I expect, which is that the program should write out
base()
derived()
base()
derived(derived)
When I try to compile it with gcc, I instead get the compiler error message
|
by: Elia Karagiannis |
last post by:
The static constructor of a derived class never gets called if it has no
other methods and the base class is only static
I have the following base class and derived class.
public class MyBase
{
protected MyBase()
{}
|
by: Edward Diener |
last post by:
Since a C++ using declaration isn't allowed in MC++, is there a way to
specify that a property, method, event, or field's access can be changed in
a derived class, ie. is protected in one class and is made public in a
derived class ?
| |
by: benliu |
last post by:
Is there an easy/special way to turn a base object into a derived
object? So for example, given the following:
class MyString : String
{
....
}
|
by: nyathancha |
last post by:
Hi,
How Do I create an instance of a derived class from an instance of a
base class, essentially wrapping up an existing base class with some
additional functionality. The reason I need this is because I am not
always able to control/create all the different constructors the base
class has. My problem can be described in code as follows ...
/* This is the base class with a whole heap of
constructors/functionality*/
public class Animal
|
by: developereo |
last post by:
Hi folks,
Can anybody shed some light on this problem?
class Interface {
public:
Interface() { ...}
virtual ~Interface() { ...}
virtual method() = 0;
};
|
by: Rahul |
last post by:
Hi Everyone,
While working with Java, i came across super() which passes values to
base class constructor from derived class constructor. I was wondering
if this could be implemented in c++ by any mechanism as super is not
supported by c++, atleast by MS vc++ 6.0.
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |