Thread: RE: [Dev-C++] Strange effect
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <no...@ya...> - 2001-10-21 13:13:01
|
-----Original Message-----
From: Jason Hardman [mailto:jas...@bt...]=20
Sent: Sunday, October 21, 2001 2:00 PM
To: Ioannis Vranos
Subject: Re: [Dev-C++] Strange effect
>It seems you don't understand what I am explaining. Do you believe that
each time you create an object of a particular class you are=20
>recreating a set of methods specifically for that object? Or=20
Yes, when we create an object all member functions are being (re)created
for that particual object.
>do you believe that there are only a single set of methods for every
object of a type, and the objects only contain the data?=20
No.
>Note that there is a distinction between an object and a class. When
you report the size of the A and C objects note that A is 1 byte
>and C is 2 bytes, which is constistent with the two char's that are
declared. If a method code was also part of the object then the values=20
>would be much bigger.
>
>Note that you are actualy over running the A object in your code and
accessing undefined memory when you access b, but it doesn't=20
>cause any errors because of the way memory is managed by the compiler.
I expect that if larger data was used then problems would=20
>crop up.
Nope, because the whole thing is a compiler defect.=20
>I might try again to get across the idea again later, because I still
think it aproaches the truth of the situation. If you want you can=20
>report the error to the team that develop gcc and see what they say,
they will know better than either of us.
>I'm sure we'll discuss this some more but I'll leave it there for now.
What happens here is that the compiler creates a C object at the place
of the A object. That is a mess and here is the proof. Run this code to
see yourself!:
#include <iostream>
=20
class A
{
public:
char a;
void test() { a=3D'A'; }
};
=20
class C: public A
{
public:
char b;
void test() { a=3D'C'; b=3D'D'; }
};
=20
=20
=20
int main()
{
using namespace std;
=20
A a;
=20
C *cp=3Dstatic_cast<C *>(&a);
=20
cp->test();
=20
cout<<"a=3D "<<cp->a<<" b=3D "<<cp->b<<endl;
=20
cout<<"sizeof(a)=3D "<<sizeof(a)<<" sizeof(*cp)=3D =
"<<sizeof(*cp)<<endl;
cout<<"Address of a=3D "<<&a<<" Address of *cp=3D "<<cp<<endl;
}
It outputs:
C:\c>temp
a=3D C b=3D D
sizeof(a)=3D 1 sizeof(*cp)=3D 2
Address of a=3D 0x241ff5f Address of *cp=3D 0x241ff5f
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.f2s.com
* Alternative URL: http://run.to/noicys
|
|
From: Ioannis V. <no...@ya...> - 2001-10-21 13:17:12
|
I forgot to mention that *static* member functions have only one instance for all the objects of a class. Non-static member functions are created each one for an object, that is any object has its own *instance* of each non-static member function. =20 =20 =20 Ioannis =20 * Ioannis Vranos * Programming pages: http://www.noicys.f2s.com <http://www.noicys.f2s.com/>=20 * Alternative URL: http://run.to/noicys -----Original Message----- From: Jason Hardman [mailto:jas...@bt...]=20 Sent: Sunday, October 21, 2001 2:00 PM To: Ioannis Vranos Subject: Re: [Dev-C++] Strange effect It seems you don't understand what I am explaining. Do you believe that each time you create an object of a particular class you are recreating a set of methods specifically for that object? Or do you believe that there are only a single set of methods for every object of a type, and the objects only contain the data? Note that there is a distinction between an object and a class. When you report the size of the A and C objects note that A is 1 byte and C is 2 bytes, which is constistent with the two char's that are declared. If a method code was also part of the object then the values would be much bigger. =20 Note that you are actualy over running the A object in your code and accessing undefined memory when you access b, but it doesn't cause any errors because of the way memory is managed by the compiler. I expect that if larger data was used then problems would crop up. =20 I might try again to get across the idea again later, because I still think it aproaches the truth of the situation. If you want you can report the error to the team that develop gcc and see what they say, they will know better than either of us. =20 I'm sure we'll discuss this some more but I'll leave it there for now. =20 Jason. ----- Original Message -----=20 From: Ioannis Vranos <mailto:no...@ya...> =20 To: Dev-C++ <mailto:dev...@li...> =20 Sent: Sunday, October 21, 2001 12:21 PM Subject: RE: [Dev-C++] Strange effect Jason, i strongly disagree with you. :) Each object has its *own* member functions and no external function is passed to them in anyway. That's why the cost of calling a member function is smaller than the cost of calling an external function. For accuracy, the compiler should behave as if this happens. :) Compiler can optimize its way as it likes as far as it follows this behavior. In our case C class is derived from A, so a C object contains a A object. Also we have public inheritance which means that all methods of A are accessible within C if not overided.=20 A C object has bigger size than a A object. Here we have a A object not a C object. If we could do these kinds of things we could create base class objects cast them to their derived types and have derived objects doing the same things having smaller size (=3Dmiracle). In our case my theory is that this happens: The compiler trying to produce even smaller size than it would in normal situation, creates common instances of member functions. Here it gets confused by the casting and a mess is created. =20 Proof that this is a compiler defect: =20 #include <iostream> =20 class A { public: char a; void test() { a=3D'A'; } }; =20 class C: public A { public: char b; void test() { a=3D'C'; b=3D'D'; } }; =20 =20 =20 int main() { using namespace std; =20 A a; =20 C *cp=3Dstatic_cast<C *>(&a); =20 cp->test(); =20 cout<<"a=3D "<<cp->a<<" b=3D "<<cp->b<<endl; =20 cout<<"sizeof(A)=3D "<<sizeof(A)<<" sizeof(C)=3D "<<sizeof(C)<<endl; } =20 =20 Ioannis =20 * Ioannis Vranos * Programming pages: http://www.noicys.f2s.com <http://www.noicys.f2s.com/>=20 * Alternative URL: http://run.to/noicys =20 -----Original Message----- From: dev...@li... [mailto:dev...@li...] On Behalf Of Jason Hardman Sent: Sunday, October 21, 2001 1:20 AM To: Dev-C++ Subject: Re: [Dev-C++] Strange effect Hi, I'm back now. I've just been to see a great Depeche Mode concert (WOW!). =20 As I mentioned I was writing the explanation of what I think is really happening quickly. I'll try again and see if it comes out right. =20 You have two classes one derived from the other, the derived class has a method that overrides one in the base. In other words two methods with the same name but different implementations. Now pretend that the two methods are actually *two* functions which are passed a pointer to a struct of some sort, which is basically how a method is implemented (don't complain, it's an analogy). When you turn pointer to the base type into an pointer to the derived type, even if it isn't a derived object, then you are telling the compiler that you want to use the object as a derived type. So when you call a method via that pointer the compiler thinks that you are really using a derived object and will try to find the correct function. Because both the functions have the same name, it will find a "test" function and pass the "this" pointer to that function that belongs to derived type, even though the "this" really points to a base object. =20 I hope you are following me so far? I understand the idea in my head but it's not easy for me to explain properly. Both the functions (methods) do something, however they don't try to access anything through the "this" pointer. That means that you won't get any problems like you would if you tried accessing something that doesn't exist in base, but is expected of derived. =20 So all that happens is the derived "test" function is called and passed a pointer it doesn't use. The function is hard coded to print out "C" in this case. =20 That is the why (AFAICT) what happens, happens. Whether the compiler should complain I don't know, it depends upon what the standard says about this case. It's only the fact that the two methods have the same name that let's this work the way it does. =20 Does all this scan? I am pretty certain that this is what is happening internally. =20 If C wasn't derived from A and you managed to cast A into C then, in theory at least, you would get the same result. Note I have not tested this. I think maybe that static_cast would not allow it to happen, so a reinterpret_cast would be needed. This is just a thought, and may not be an actual truth. =20 Regards, Jason. |
|
From: Daniel G. <sp...@ho...> - 2001-10-22 17:55:42
|
<html><div style='background-color:'><P>An object holds it's own data members but does not have it's own copy of member functions - there is one instance of a member function that is used by all (since it does not change, while data members do).</P> <P> </P> <DIV>> > -----Original Message----- </DIV> <DIV></DIV>> > From: dev...@li... <DIV></DIV>> > [mailto:dev...@li...] On Behalf <DIV></DIV>> > Of Jason Hardman <DIV></DIV>> > Sent: Monday, October 22, 2001 12:10 AM <DIV></DIV>> > To: Dev-C++ <DIV></DIV>> > Subject: Re: [Dev-C++] Strange effect <DIV></DIV>> > <DIV></DIV>> > <DIV></DIV>> > Is this just your belief or can you prove that this is the <DIV></DIV>> > case from some external referece or knowledgable party (other <DIV></DIV>> > than yourself)? <DIV></DIV>> > <DIV></DIV>> > As I have been given to understand it the case of there only <DIV></DIV>> > being one set of methods existing in memory for a class, and <DIV></DIV>> > then each method being passed a hidden pointer to the object <DIV></DIV>> > is the true case. Hence the existence of the "this" pointer <DIV></DIV>> > for use in a class. I would also like to mention that the way <DIV></DIV>> > you would have it is unecessarily wasteful of resources. The <DIV></DIV>> > issue becomes more complex with virtual methods and such, but <DIV></DIV>> > I won't go into that right now. <DIV></DIV>> > <DIV></DIV>> > I wouldn't mind if someone else who is reading this <DIV></DIV>> > discussion, and is knowledgable about the subject, putting us <DIV></DIV>> > both on the correct path. <DIV></DIV>> > <DIV></DIV>> > Jason. <DIV></DIV>> > <DIV></DIV>> > ----- Original Message ----- <DIV></DIV>> > From: "Ioannis Vranos" <NO...@YA...> <DIV></DIV>> > To: "Dev-C++" <DEV...@LI...> <DIV></DIV>> > Sent: Sunday, October 21, 2001 3:14 PM <DIV></DIV>> > Subject: RE: [Dev-C++] Strange effect <DIV></DIV>> > <DIV></DIV>> > <DIV></DIV>> > Yes, when we create an object all member functions are being <DIV></DIV>> > (re)created for that particual object. <DIV></DIV>> > <DIV></DIV>> > <DIV></DIV>> > <DIV></DIV>> > >do you believe that there are only a single set of methods for every <DIV></DIV>> > object of a type, and the objects only contain the data? <DIV></DIV>> > <DIV></DIV>> > <DIV></DIV>> > No. <DIV></DIV>> > <DIV></DIV>> > <DIV></DIV>> > <DIV></DIV>> > _______________________________________________ <DIV></DIV>> > Dev-cpp-users mailing list <DIV></DIV>> > Dev...@li... <DIV></DIV>> > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users <DIV></DIV>> > <DIV></DIV>> <DIV></DIV>> <DIV></DIV>>_______________________________________________ <DIV></DIV>>Dev-cpp-users mailing list <DIV></DIV>>Dev...@li... <DIV></DIV>>https://lists.sourceforge.net/lists/listinfo/dev-cpp-users <DIV></DIV></div><br clear=all><hr>Get your FREE download of MSN Explorer at <a href='http://go.msn.com/bql/hmtag_itl_EN.asp'>http://explorer.msn.com</a><br></html> |
|
From: Ioannis V. <no...@ya...> - 2001-10-22 19:42:39
|
I do not think this is true. Every object has its own member functions. Except of that, here wrong data members are being created too. =20 =20 Ioannis =20 * Ioannis Vranos * Programming pages: http://www.noicys.f2s.com <http://www.noicys.f2s.com/>=20 * Alternative URL: http://run.to/noicys =20 -----Original Message----- From: dev...@li... [mailto:dev...@li...] On Behalf Of Daniel Glenfield Sent: Monday, October 22, 2001 7:56 PM To: dev...@li... Subject: RE: [Dev-C++] Strange effect An object holds it's own data members but does not have it's own copy of member functions - there is one instance of a member function that is used by all (since it does not change, while data members do). |
|
From: Daniel G. <sp...@ho...> - 2001-10-22 20:30:43
|
<html><div style='background-color:'><DIV> <P>I am 100% certain that each object does NOT have it's own copy of the member functions. What reason would there be?<BR><BR></P></DIV> <DIV></DIV> <DIV></DIV>>From: "Ioannis Vranos" <NO...@YA...> <DIV></DIV>>To: "Dev-C++" <DEV...@LI...> <DIV></DIV>>Subject: RE: [Dev-C++] Strange effect <DIV></DIV>>Date: Mon, 22 Oct 2001 22:44:21 +0200 <DIV></DIV>> <DIV></DIV>>I do not think this is true. Every object has its own member functions. <DIV></DIV>>Except of that, here wrong data members are being created too. <DIV></DIV>> <DIV></DIV>> <DIV></DIV>>Ioannis <DIV></DIV>> <DIV></DIV>>* Ioannis Vranos <DIV></DIV>>* Programming pages: http://www.noicys.f2s.com <DIV></DIV>> <DIV></DIV>>* Alternative URL: http://run.to/noicys <DIV></DIV>> <DIV></DIV>> <DIV></DIV>>-----Original Message----- <DIV></DIV>>From: dev...@li... <DIV></DIV>>[mailto:dev...@li...] On Behalf Of Daniel <DIV></DIV>>Glenfield <DIV></DIV>>Sent: Monday, October 22, 2001 7:56 PM <DIV></DIV>>To: dev...@li... <DIV></DIV>>Subject: RE: [Dev-C++] Strange effect <DIV></DIV>> <DIV></DIV>> <DIV></DIV>> <DIV></DIV>>An object holds it's own data members but does not have it's own copy of <DIV></DIV>>member functions - there is one instance of a member function that is <DIV></DIV>>used by all (since it does not change, while data members do). <DIV></DIV>> <DIV></DIV></div><br clear=all><hr>Get your FREE download of MSN Explorer at <a href='http://go.msn.com/bql/hmtag_itl_EN.asp'>http://explorer.msn.com</a><br></html> |
|
From: Ioannis V. <no...@ya...> - 2001-10-22 20:41:44
|
:) That every object has its own data members whhich these functions manipulate. Except of that, for what static data members and static member functions exist for? =20 =20 =20 Ioannis =20 * Ioannis Vranos * Programming pages: http://www.noicys.f2s.com <http://www.noicys.f2s.com/>=20 * Alternative URL: http://run.to/noicys -----Original Message----- From: dev...@li... [mailto:dev...@li...] On Behalf Of Daniel Glenfield Sent: Monday, October 22, 2001 10:30 PM To: dev...@li... Subject: RE: [Dev-C++] Strange effect I am 100% certain that each object does NOT have it's own copy of the member functions. What reason would there be? >From: "Ioannis Vranos"=20 >To: "Dev-C++"=20 >Subject: RE: [Dev-C++] Strange effect=20 >Date: Mon, 22 Oct 2001 22:44:21 +0200=20 >=20 >I do not think this is true. Every object has its own member functions. >Except of that, here wrong data members are being created too.=20 >=20 >=20 >Ioannis=20 >=20 >* Ioannis Vranos=20 >* Programming pages: http://www.noicys.f2s.com=20 >=20 >* Alternative URL: http://run.to/noicys=20 >=20 >=20 >-----Original Message-----=20 >From: dev...@li...=20 >[mailto:dev...@li...] On Behalf Of Daniel=20 >Glenfield=20 >Sent: Monday, October 22, 2001 7:56 PM=20 >To: dev...@li...=20 >Subject: RE: [Dev-C++] Strange effect=20 >=20 >=20 >=20 >An object holds it's own data members but does not have it's own copy of=20 >member functions - there is one instance of a member function that is=20 >used by all (since it does not change, while data members do).=20 >=20 _____ =20 Get your FREE download of MSN Explorer at http://explorer.msn.com <http://go.msn.com/bql/hmtag_itl_EN.asp>=20 _______________________________________________ Dev-cpp-users mailing list Dev...@li... https://lists.sourceforge.net/lists/listinfo/dev-cpp-users=20 |
|
From: Daniel G. <sp...@ho...> - 2001-10-22 20:48:37
|
<html><div style='background-color:'><DIV> <P><BR><BR></P> <DIV> <DIV></DIV> <P>objects have their own data members, they share the member functions in the class they are a part of. They do not have their own functions, this I am sure of.<BR><BR></P></DIV></DIV> <DIV></DIV> <DIV></DIV> <DIV></DIV>>From: "Ioannis Vranos" <NO...@YA...> <DIV></DIV> <DIV></DIV>>To: "Dev-C++" <DEV...@LI...> <DIV></DIV> <DIV></DIV>>Subject: RE: [Dev-C++] Strange effect <DIV></DIV> <DIV></DIV>>Date: Mon, 22 Oct 2001 23:43:25 +0200 <DIV></DIV> <DIV></DIV>> <DIV></DIV> <DIV></DIV>>:) That every object has its own data members whhich these functions <DIV></DIV> <DIV></DIV>>manipulate. Except of that, for what static data members and static <DIV></DIV> <DIV></DIV>>member functions exist for? <DIV></DIV> <DIV></DIV>> <DIV></DIV> <DIV></DIV>> <DIV></DIV> <DIV></DIV>> <DIV></DIV> <DIV></DIV>>Ioannis <DIV></DIV> <DIV></DIV>> <DIV></DIV> <DIV></DIV>>* Ioannis Vranos <DIV></DIV> <DIV></DIV>>* Programming pages: http://www.noicys.f2s.com <DIV></DIV> <DIV></DIV>> <DIV></DIV> <DIV></DIV>>* Alternative URL: http://run.to/noicys <DIV></DIV> <DIV></DIV>> <DIV></DIV> <DIV></DIV>>-----Original Message----- <DIV></DIV> <DIV></DIV>>From: dev...@li... <DIV></DIV> <DIV></DIV>>[mailto:dev...@li...] On Behalf Of Daniel <DIV></DIV> <DIV></DIV>>Glenfield <DIV></DIV> <DIV></DIV>>Sent: Monday, October 22, 2001 10:30 PM <DIV></DIV> <DIV></DIV>>To: dev...@li... <DIV></DIV> <DIV></DIV>>Subject: RE: [Dev-C++] Strange effect <DIV></DIV> <DIV></DIV>> <DIV></DIV> <DIV></DIV>> <DIV></DIV> <DIV></DIV>> <DIV></DIV> <DIV></DIV>>I am 100% certain that each object does NOT have it's own copy of the <DIV></DIV> <DIV></DIV>>member functions. What reason would there be? <DIV></DIV> <DIV></DIV>> <DIV></DIV> <DIV></DIV>> <DIV></DIV> <DIV></DIV>> <DIV></DIV> <DIV></DIV>> >From: "Ioannis Vranos" <DIV></DIV> <DIV></DIV>> >To: "Dev-C++" <DIV></DIV> <DIV></DIV>> >Subject: RE: [Dev-C++] Strange effect <DIV></DIV> <DIV></DIV>> >Date: Mon, 22 Oct 2001 22:44:21 +0200 <DIV></DIV> <DIV></DIV>> > <DIV></DIV> <DIV></DIV>> >I do not think this is true. Every object has its own member functions. <DIV></DIV> <DIV></DIV>> <DIV></DIV> <DIV></DIV>> >Except of that, here wrong data members are being created too. <DIV></DIV> <DIV></DIV>> > <DIV></DIV> <DIV></DIV>> > <DIV></DIV> <DIV></DIV>> >Ioannis <DIV></DIV> <DIV></DIV>> > <DIV></DIV> <DIV></DIV>> >* Ioannis Vranos <DIV></DIV> <DIV></DIV>> >* Programming pages: http://www.noicys.f2s.com <DIV></DIV> <DIV></DIV>> > <DIV></DIV> <DIV></DIV>> >* Alternative URL: http://run.to/noicys <DIV></DIV> <DIV></DIV>> > <DIV></DIV> <DIV></DIV>> > <DIV></DIV> <DIV></DIV>> >-----Original Message----- <DIV></DIV> <DIV></DIV>> >From: dev...@li... <DIV></DIV> <DIV></DIV>> >[mailto:dev...@li...] On Behalf Of Daniel <DIV></DIV> <DIV></DIV>> >Glenfield <DIV></DIV> <DIV></DIV>> >Sent: Monday, October 22, 2001 7:56 PM <DIV></DIV> <DIV></DIV>> >To: dev...@li... <DIV></DIV> <DIV></DIV>> >Subject: RE: [Dev-C++] Strange effect <DIV></DIV> <DIV></DIV>> > <DIV></DIV> <DIV></DIV>> > <DIV></DIV> <DIV></DIV>> > <DIV></DIV> <DIV></DIV>> >An object holds it's own data members but does not have it's own copy <DIV></DIV> <DIV></DIV>>of <DIV></DIV> <DIV></DIV>> >member functions - there is one instance of a member function that is <DIV></DIV> <DIV></DIV>> >used by all (since it does not change, while data members do). <DIV></DIV> <DIV></DIV>> > <DIV></DIV> <DIV></DIV>> <DIV></DIV> <DIV></DIV>> _____ <DIV></DIV> <DIV></DIV>> <DIV></DIV> <DIV></DIV>>Get your FREE download of MSN Explorer at http://explorer.msn.com <DIV></DIV> <DIV></DIV>> <DIV></DIV> <DIV></DIV>>_______________________________________________ Dev-cpp-users mailing <DIV></DIV> <DIV></DIV>>list Dev...@li... <DIV></DIV> <DIV></DIV>>https://lists.sourceforge.net/lists/listinfo/dev-cpp-users <DIV></DIV> <DIV></DIV>> <DIV></DIV> <DIV></DIV> <DIV></DIV></div><br clear=all><hr>Get your FREE download of MSN Explorer at <a href='http://go.msn.com/bql/hmtag_itl_EN.asp'>http://explorer.msn.com</a><br></html> |
|
From: Jason H. <jas...@bt...> - 2001-10-23 03:32:08
|
I'm glad someone else agrees with me. :) Now onto the static issue, "In C++, when static is used on a class = *data* member, it causes only one copy of that member to be shared by = all objects of its class", (C/C++ Programmer's Reference, second edition = - Herbert Schildt). "Member functions may also be declared as static. There are several = restrictions placed on static member functions. They may only directly = refer to other static members of the class. (Of course, global functions = and data my be accessed by static member functions.) A static member = function does not have a this pointer....", (C++ The Complete Reference, = Third Edition - Herbert Schildt, p.315). The reason that a static member = can't access non-static members is because it doesn't have a this = pointer and has no frame of reference to get to the data, but a static = data member is a single defined instance which therefore has a specific = place in memory that can be directly refered to. There still only needs = to be one copy of the member function. In the end a static member is a = special case used to overcome specific problems. Jason. ----- Original Message -----=20 From: Daniel Glenfield=20 To: dev...@li...=20 Sent: Monday, October 22, 2001 9:48 PM Subject: RE: [Dev-C++] Strange effect objects have their own data members, they share the member functions = in the class they are a part of. They do not have their own functions, = this I am sure of. >From: "Ioannis Vranos"=20 >To: "Dev-C++"=20 >Subject: RE: [Dev-C++] Strange effect=20 >Date: Mon, 22 Oct 2001 23:43:25 +0200=20 >=20 >:) That every object has its own data members whhich these functions=20 >manipulate. Except of that, for what static data members and static=20 >member functions exist for?=20 >=20 >=20 >=20 >Ioannis=20 >=20 >* Ioannis Vranos=20 >* Programming pages: http://www.noicys.f2s.com=20 >=20 >* Alternative URL: http://run.to/noicys=20 >=20 >-----Original Message-----=20 >From: dev...@li...=20 >[mailto:dev...@li...] On Behalf Of = Daniel=20 >Glenfield=20 >Sent: Monday, October 22, 2001 10:30 PM=20 >To: dev...@li...=20 >Subject: RE: [Dev-C++] Strange effect=20 >=20 >=20 >=20 >I am 100% certain that each object does NOT have it's own copy of the = >member functions. What reason would there be?=20 >=20 >=20 >=20 > >From: "Ioannis Vranos"=20 > >To: "Dev-C++"=20 > >Subject: RE: [Dev-C++] Strange effect=20 > >Date: Mon, 22 Oct 2001 22:44:21 +0200=20 > >=20 > >I do not think this is true. Every object has its own member = functions.=20 >=20 > >Except of that, here wrong data members are being created too.=20 > >=20 > >=20 > >Ioannis=20 > >=20 > >* Ioannis Vranos=20 > >* Programming pages: http://www.noicys.f2s.com=20 > >=20 > >* Alternative URL: http://run.to/noicys=20 > >=20 > >=20 > >-----Original Message-----=20 > >From: dev...@li...=20 > >[mailto:dev...@li...] On Behalf Of = Daniel=20 > >Glenfield=20 > >Sent: Monday, October 22, 2001 7:56 PM=20 > >To: dev...@li...=20 > >Subject: RE: [Dev-C++] Strange effect=20 > >=20 > >=20 > >=20 > >An object holds it's own data members but does not have it's own = copy=20 >of=20 > >member functions - there is one instance of a member function that = is=20 > >used by all (since it does not change, while data members do).=20 > >=20 >=20 > _____=20 >=20 >Get your FREE download of MSN Explorer at http://explorer.msn.com=20 >=20 >_______________________________________________ Dev-cpp-users mailing = >list Dev...@li...=20 >https://lists.sourceforge.net/lists/listinfo/dev-cpp-users=20 >=20 -------------------------------------------------------------------------= ----- Get your FREE download of MSN Explorer at http://explorer.msn.com _______________________________________________ Dev-cpp-users mailing = list Dev...@li... = https://lists.sourceforge.net/lists/listinfo/dev-cpp-users |
|
From: Ioannis V. <no...@ya...> - 2001-10-23 08:36:53
|
Finally i agree with you. You are right, every member function has only one instance. I understood it indirectly, from the way pointers to member functions work. Well although i thought i know much about C++, probably there are details i miss. :) =20 =20 =20 Ioannis =20 * Ioannis Vranos * Programming pages: http://www.noicys.f2s.com <http://www.noicys.f2s.com/>=20 * Alternative URL: http://run.to/noicys =20 -----Original Message----- From: dev...@li... [mailto:dev...@li...] On Behalf Of Jason Hardman Sent: Tuesday, October 23, 2001 5:32 AM To: Dev-C++ Subject: Re: [Dev-C++] Strange effect I'm glad someone else agrees with me. :) =20 Now onto the static issue, "In C++, when static is used on a class *data* member, it causes only one copy of that member to be shared by all objects of its class", (C/C++ Programmer's Reference, second edition - Herbert Schildt). =20 "Member functions may also be declared as static. There are several restrictions placed on static member functions. They may only directly refer to other static members of the class. (Of course, global functions and data my be accessed by static member functions.) A static member function does not have a this pointer....", (C++ The Complete Reference, Third Edition - Herbert Schildt, p.315). The reason that a static member can't access non-static members is because it doesn't have a this pointer and has no frame of reference to get to the data, but a static data member is a single defined instance which therefore has a specific place in memory that can be directly refered to. There still only needs to be one copy of the member function. In the end a static member is a special case used to overcome specific problems. =20 Jason. =20 =20 ----- Original Message -----=20 From: Daniel <mailto:sp...@ho...> Glenfield=20 To: dev...@li...=20 Sent: Monday, October 22, 2001 9:48 PM Subject: RE: [Dev-C++] Strange effect objects have their own data members, they share the member functions in the class they are a part of. They do not have their own functions, this I am sure of. >From: "Ioannis Vranos"=20 >To: "Dev-C++"=20 >Subject: RE: [Dev-C++] Strange effect=20 >Date: Mon, 22 Oct 2001 23:43:25 +0200=20 >=20 >:) That every object has its own data members whhich these functions=20 >manipulate. Except of that, for what static data members and static=20 >member functions exist for?=20 >=20 >=20 >=20 >Ioannis=20 >=20 >* Ioannis Vranos=20 >* Programming pages: http://www.noicys.f2s.com=20 >=20 >* Alternative URL: http://run.to/noicys=20 >=20 >-----Original Message-----=20 >From: dev...@li...=20 >[mailto:dev...@li...] On Behalf Of Daniel=20 >Glenfield=20 >Sent: Monday, October 22, 2001 10:30 PM=20 >To: dev...@li...=20 >Subject: RE: [Dev-C++] Strange effect=20 >=20 >=20 >=20 >I am 100% certain that each object does NOT have it's own copy of the=20 >member functions. What reason would there be?=20 >=20 >=20 >=20 > >From: "Ioannis Vranos"=20 > >To: "Dev-C++"=20 > >Subject: RE: [Dev-C++] Strange effect=20 > >Date: Mon, 22 Oct 2001 22:44:21 +0200=20 > >=20 > >I do not think this is true. Every object has its own member functions.=20 >=20 > >Except of that, here wrong data members are being created too.=20 > >=20 > >=20 > >Ioannis=20 > >=20 > >* Ioannis Vranos=20 > >* Programming pages: http://www.noicys.f2s.com=20 > >=20 > >* Alternative URL: http://run.to/noicys=20 > >=20 > >=20 > >-----Original Message-----=20 > >From: dev...@li...=20 > >[mailto:dev...@li...] On Behalf Of Daniel=20 > >Glenfield=20 > >Sent: Monday, October 22, 2001 7:56 PM=20 > >To: dev...@li...=20 > >Subject: RE: [Dev-C++] Strange effect=20 > >=20 > >=20 > >=20 > >An object holds it's own data members but does not have it's own copy >of=20 > >member functions - there is one instance of a member function that is > >used by all (since it does not change, while data members do).=20 > >=20 >=20 > _____=20 >=20 >Get your FREE download of MSN Explorer at http://explorer.msn.com=20 >=20 >_______________________________________________ Dev-cpp-users mailing=20 >list Dev...@li...=20 >https://lists.sourceforge.net/lists/listinfo/dev-cpp-users=20 >=20 _____ =20 Get your FREE download of MSN Explorer at http://explorer.msn.com <http://go.msn.com/bql/hmtag_itl_EN.asp>=20 _______________________________________________ Dev-cpp-users mailing list Dev...@li... https://lists.sourceforge.net/lists/listinfo/dev-cpp-users |
|
From: Jason H. <jas...@bt...> - 2001-10-21 22:10:29
|
Is this just your belief or can you prove that this is the case from some external referece or knowledgable party (other than yourself)? As I have been given to understand it the case of there only being one set of methods existing in memory for a class, and then each method being passed a hidden pointer to the object is the true case. Hence the existence of the "this" pointer for use in a class. I would also like to mention that the way you would have it is unecessarily wasteful of resources. The issue becomes more complex with virtual methods and such, but I won't go into that right now. I wouldn't mind if someone else who is reading this discussion, and is knowledgable about the subject, putting us both on the correct path. Jason. ----- Original Message ----- From: "Ioannis Vranos" <no...@ya...> To: "Dev-C++" <dev...@li...> Sent: Sunday, October 21, 2001 3:14 PM Subject: RE: [Dev-C++] Strange effect Yes, when we create an object all member functions are being (re)created for that particual object. >do you believe that there are only a single set of methods for every object of a type, and the objects only contain the data? No. |
|
From: Ioannis V. <no...@ya...> - 2001-10-22 11:27:50
|
If i recall correctly, you have Special Edition. Take a look at 10.2.1 on page 224. But if you notice in my examples even wrong data members are being created! Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.f2s.com * Alternative URL: http://run.to/noicys > -----Original Message----- > From: dev...@li...=20 > [mailto:dev...@li...] On Behalf=20 > Of Jason Hardman > Sent: Monday, October 22, 2001 12:10 AM > To: Dev-C++ > Subject: Re: [Dev-C++] Strange effect >=20 >=20 > Is this just your belief or can you prove that this is the=20 > case from some external referece or knowledgable party (other=20 > than yourself)? >=20 > As I have been given to understand it the case of there only=20 > being one set of methods existing in memory for a class, and=20 > then each method being passed a hidden pointer to the object=20 > is the true case. Hence the existence of the "this" pointer=20 > for use in a class. I would also like to mention that the way=20 > you would have it is unecessarily wasteful of resources. The=20 > issue becomes more complex with virtual methods and such, but=20 > I won't go into that right now. >=20 > I wouldn't mind if someone else who is reading this=20 > discussion, and is knowledgable about the subject, putting us=20 > both on the correct path. >=20 > Jason. >=20 > ----- Original Message ----- > From: "Ioannis Vranos" <no...@ya...> > To: "Dev-C++" <dev...@li...> > Sent: Sunday, October 21, 2001 3:14 PM > Subject: RE: [Dev-C++] Strange effect >=20 >=20 > Yes, when we create an object all member functions are being=20 > (re)created for that particual object. >=20 >=20 >=20 > >do you believe that there are only a single set of methods for every > object of a type, and the objects only contain the data? >=20 >=20 > No. >=20 >=20 >=20 > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users >=20 |
|
From: Jason H. <jas...@bt...> - 2001-10-23 03:11:58
|
I do not see how that passage would prove that each object has it's own copy of a member fuction. In fact the oposite: 'A class member function always "knows" for which object it was invoked', which implies that there is one function and it is made aware of the specific object it is being used upon. BTW I don't have a special edition, but I do have the third edition of Stroustrup. As for the creation of the variable, there is a distinction between the creation of the space for the variable and the initialisation of it. What you are doing in your code is writing to where the compiler thinks the variable should be, even though for that object it hasn't been defined in memory. Technically you are writing to unallocated memory. If the variables were bigger then you would risk overwriting into something else, or causing an access violation. Jason. ----- Original Message ----- From: "Ioannis Vranos" <no...@ya...> To: "Dev-C++" <dev...@li...> Sent: Monday, October 22, 2001 1:29 PM Subject: RE: [Dev-C++] Strange effect If i recall correctly, you have Special Edition. Take a look at 10.2.1 on page 224. But if you notice in my examples even wrong data members are being created! Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.f2s.com * Alternative URL: http://run.to/noicys > -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of Jason Hardman > Sent: Monday, October 22, 2001 12:10 AM > To: Dev-C++ > Subject: Re: [Dev-C++] Strange effect > > > Is this just your belief or can you prove that this is the > case from some external referece or knowledgable party (other > than yourself)? > > As I have been given to understand it the case of there only > being one set of methods existing in memory for a class, and > then each method being passed a hidden pointer to the object > is the true case. Hence the existence of the "this" pointer > for use in a class. I would also like to mention that the way > you would have it is unecessarily wasteful of resources. The > issue becomes more complex with virtual methods and such, but > I won't go into that right now. > > I wouldn't mind if someone else who is reading this > discussion, and is knowledgable about the subject, putting us > both on the correct path. > > Jason. > > ----- Original Message ----- > From: "Ioannis Vranos" <no...@ya...> > To: "Dev-C++" <dev...@li...> > Sent: Sunday, October 21, 2001 3:14 PM > Subject: RE: [Dev-C++] Strange effect > > > Yes, when we create an object all member functions are being > (re)created for that particual object. > > > > >do you believe that there are only a single set of methods for every > object of a type, and the objects only contain the data? > > > No. > > > > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > _______________________________________________ Dev-cpp-users mailing list Dev...@li... https://lists.sourceforge.net/lists/listinfo/dev-cpp-users |