Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > So it has been proven

Reply
Thread Tools

So it has been proven

 
 
Paul
Guest
Posts: n/a
 
      03-02-2011

>"gwowen" <> wrote in message
>news:796f066f-131f-4e80-a8b5-...
>On Mar 1, 8:56 pm, "Paul" <pchris...@yahoo.co.uk> wrote:
>> Seems like a fair assessment.
>> Also note that (A::*)() can only call itself with objects of type A or in
>> a
>> derived heirarchy containing A.


>Unless you use a cast. If you subvert the type system, you can do
>what the hell you want.


No you cannot call anything , as I said you can only call objects of A or
derived.

>>(*)(A*) does not have this restraint as the
>> pointer parameter can theoretically point to any type of object.


>Only if you use a cast. If you subvert the type system, you can do
>what the hell you want.


So casting is "subverting the type system" in your mind?
You cannot do what the hell you want. There are limits to what a program can
do , and I pointed out there are limits to what a member fucntion can do
(casting or no casting).

If you were correct I would bang up a quick program, with a coupe of casts
in it , to go me a cuppa tea. LOL
The three people who posted after you have nothing intelligent to say, they
are just attempting to dilute the argument into a troll. Obviously they have
been defeated and they have nothing more to say than whine , moan and meke
derogatory insults, One of them said it has all been said already, but I
don't recall him proposing any argumental proof that an ordinary function is
the same as a member function ,He is obviously a liar too. It's up to you if
you want to think like those 3 idiots.

*shrug*



 
Reply With Quote
 
 
 
 
gwowen
Guest
Posts: n/a
 
      03-02-2011
On Mar 2, 11:46*am, "Paul" <pchris...@yahoo.co.uk> wrote:
> >"gwowen" <gwo...@gmail.com> wrote in message
> >news:796f066f-131f-4e80-a8b5-....
> >On Mar 1, 8:56 pm, "Paul" <pchris...@yahoo.co.uk> wrote:
> >> Seems like a fair assessment.
> >> Also note that (A::*)() can only call itself with objects of type A orin
> >> a
> >> derived heirarchy containing A.

> >Unless you use a cast. *If you subvert the type system, you can do
> >what the hell you want.

>
> No you cannot call anything , as I said you can only call objects of A or
> derived.


Really? What does the following do?

#include <iostream>
class A {
int num;
int another_num;
public:
int f() { return num;}
void setnum(int x) {num = x;}
};

int main()
{
int z = 4;
A* foo = reinterpret_cast<A*> (&z);

std::cout << foo->f() << std::endl;
foo->setnum(7);
std::cout << z << std::endl;
}
 
Reply With Quote
 
 
 
 
Paul
Guest
Posts: n/a
 
      03-02-2011

>"gwowen" <> wrote in message
>news:24201502-fc44-4c0f-9af3-...
>On Mar 2, 11:46 am, "Paul" <pchris...@yahoo.co.uk> wrote:
>> >"gwowen" <gwo...@gmail.com> wrote in message
>> >news:796f066f-131f-4e80-a8b5-...
>> >On Mar 1, 8:56 pm, "Paul" <pchris...@yahoo.co.uk> wrote:
>> >> Seems like a fair assessment.
>> >> Also note that (A::*)() can only call itself with objects of type A or
>> >> in
>> >> a
>> >> derived heirarchy containing A.
>> >Unless you use a cast. If you subvert the type system, you can do
>> >what the hell you want.

>>
>> No you cannot call anything , as I said you can only call objects of A or
>> derived.


>Really? What does the following do?


You tell us what it does, its your code.
As I said , with member functions you can only only call objects of the type
or derived ( see the C++ standards) .



>#include <iostream>
>class A {
> int num;
> int another_num;
>public:
> int f() { return num;}
> void setnum(int x) {num = x;}
>};


>int main(){
> int z = 4;
> A* foo = reinterpret_cast<A*> (&z);
> std::cout << foo->f() << std::endl;
> foo->setnum(7);
> std::cout << z << std::endl;
>}


This just looks like a buggy mess to me, I have no interest in such code.


 
Reply With Quote
 
gwowen
Guest
Posts: n/a
 
      03-02-2011
On Mar 2, 12:47*pm, "Paul" <pchris...@yahoo.co.uk> wrote:
> >"gwowen" <gwo...@gmail.com> wrote in message
> >news:24201502-fc44-4c0f-9af3-....
> >On Mar 2, 11:46 am, "Paul" <pchris...@yahoo.co.uk> wrote:
> >> >"gwowen" <gwo...@gmail.com> wrote in message
> >> >news:796f066f-131f-4e80-a8b5-...
> >> >On Mar 1, 8:56 pm, "Paul" <pchris...@yahoo.co.uk> wrote:
> >> >> Seems like a fair assessment.
> >> >> Also note that (A::*)() can only call itself with objects of type Aor
> >> >> in
> >> >> a
> >> >> derived heirarchy containing A.
> >> >Unless you use a cast. If you subvert the type system, you can do
> >> >what the hell you want.

>
> >> No you cannot call anything , as I said you can only call objects of Aor
> >> derived.

> >Really? *What does the following do?

>
> You tell us what it does, its your code.
> As I said , with member functions you can only only call objects of the type
> or derived ( see the C++ standards) .
>
> >#include <iostream>
> >class A {
> > *int num;
> > *int another_num;
> >public:
> > *int f() { return num;}
> > *void setnum(int x) {num = x;}
> >};
> >int main(){
> > *int z = 4;
> > *A* foo = reinterpret_cast<A*> (&z);
> > *std::cout << foo->f() << std::endl;
> > *foo->setnum(7);
> > *std::cout << z << std::endl;
> >}

>
> This just looks like a buggy mess to me, I have no interest in such code.


Thank you. That's given me the first genuine belly laugh of what was
quite a stressful day.
 
Reply With Quote
 
gwowen
Guest
Posts: n/a
 
      03-02-2011
On Mar 2, 12:15*pm, gwowen <gwo...@gmail.com> wrote:

Sorry this:

> class A {
> * int num;
> * int another_num;
> public:
> * int f() { return num;}
> * void setnum(int x) {num = x;}
>
> };


should have said this:

class A {
public:
int num;
int another_num;
int f() { return num;}
void setnum(int x) {num = x;}
};

although I'd be surprised if it made any difference in practice.
Forgot my POD type can't have private data members. Bad me.
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      03-02-2011

>"gwowen" <> wrote in message
>news:9bb268bb-a7fe-476d-b268-...
>On Mar 2, 12:15 pm, gwowen <gwo...@gmail.com> wrote:


>Sorry this:


>> class A {
>> int num;
>> int another_num;
>> public:
>> int f() { return num;}
>> void setnum(int x) {num = x;}
>>
>> };


>should have said this:


>class A {
>public:
> int num;
> int another_num;
> int f() { return num;}
> void setnum(int x) {num = x;}
>};


>although I'd be surprised if it made any difference in practice.
>Forgot my POD type can't have private data members. Bad me.


What exactly are you trying to do ?
We can tell you if it can be done and if so how to do it.

 
Reply With Quote
 
gwowen
Guest
Posts: n/a
 
      03-02-2011
On Mar 2, 2:42*pm, "Paul" <pchris...@yahoo.co.uk> wrote:
> What exactly are you trying to do ?


It is you who claims my code is buggy, so you tell me. Have you tried
to compile and run the code. Is it standard compliant? Why? Does it
compile on Comeau in strict mode?

What does it do? How does it do it?

#include<iostream>
class A {
public:
int num;
int another_num;
int f() { return num;}
void setnum(int x) {num = x;}
};
int main()
{
int z = 4;
A* foo = reinterpret_cast<A*> (&z);

std::cout << foo->f() << std::endl;
foo->setnum(7);
std::cout << z << std::endl;
}

Now, the million dollar question, how does its behaviour differ from
the following code?

#include<iostream>
class A {
public:
int num;
int another_num;
};

int f(A*const this_) { return this_->num;}
void setnum(A*const this_,int x) {this_->num = x;}

int main()
{
int z = 4;
A* foo = reinterpret_cast<A*> (&z);

std::cout << f(foo) << std::endl;
setnum(foo,7);
std::cout << z << std::endl;
}
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      03-02-2011

>"gwowen" <> wrote in message
>news:440bcd0a-7254-4eaa-8a30-...
>On Mar 2, 2:42 pm, "Paul" <pchris...@yahoo.co.uk> wrote:
>> What exactly are you trying to do ?


>It is you who claims my code is buggy, so you tell me. Have you tried
>to compile and run the code. Is it standard compliant? Why? Does it
>compile on Comeau in strict mode?


>What does it do? How does it do it?


If you cannot even explain your own code , or even its intended purpose then
I don't give a **** what it does or how it does it TBH.

<snip>

 
Reply With Quote
 
gwowen
Guest
Posts: n/a
 
      03-02-2011
On Mar 2, 4:03*pm, "Paul" <pchris...@yahoo.co.uk> wrote:
> If you cannot even explain your own code , or even its intended purpose then
> I don't give a **** what it does or how it does it TBH.


I know what it does, thanks. It calls a non-static member function,
without an instantiated object, with predictable defined results.
Thereby showing that you don't need an object to call a (non-virtual)
member function [You do need one for virtual member functions, for
obvious reasons].

i.e. It does what you have repeatedly claimed to be impossible.

I'll explain it to you, now So pay attention

/*
class A is a POD-struct (no non-public data members, no virtual
functions).
This means that the address of any A object is the address of its
first data member
(an int) which means that I can type-pun between A* and int*
i.e. if sizeof(int) = N, the first N bytes of an "A object", is the
value representation of "num"
*/
class A {
public:
int num;
int another_num;
/*
This getter function returns the value of the first data member,
which for the reasons mentioned above is the same as *(int*)
(address of object)
*/
int f() { return num;}
/*
This setter function sets the first sizeof(N) bytes of the object
to the value representation of x
*/
void setnum(int x) {num = x;}
};



int main()
{
int z = 4;
/* tell the compiler to pretend the object at &z, is an A, not an
int */
A* foo = reinterpret_cast<A*> (&z);

/* Call the getter member function on our fictional A object. This
is ok, because we only access the A->num data member, and we know
thats precisely the storage occupied by z - which conveniently is the
same type. So the first N bytes at that address are the integer value
representation of 4 (the value of z)/ Since the methods aren't
virtual, we don't need to access a vtable, or any type information,
which is good, as there isn't any. We guarantee this compile-time
dispatch because A is POD. That's why I had to have public data
members */
std::cout << f(foo) << std::endl;
/* Call the setter member function on our fictional A object. This
is also ok, for all the same reasons. We set the first N bytes at
address foo = &z to the value representation of 7 */
setnum(foo,7);
/* And now, as if by magic, z is 7. The end. No A object was
created by this code. I lied to the compiler that there was one at
&z. I never allocated any storage, for example for the any data
member A::another_num */
std::cout << z << std::endl;
}

result: the code prints 4, then 7.

See also, the side-by-side code at
http://publib.boulder.ibm.com/infoce...ef/cplr035.htm
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      03-02-2011

>"gwowen" <> wrote in message
>news:b6042479-9aff-49f4-b5ff-...
>On Mar 2, 4:03 pm, "Paul" <pchris...@yahoo.co.uk> wrote:
>> If you cannot even explain your own code , or even its intended purpose
>> then
>> I don't give a **** what it does or how it does it TBH.


>I know what it does, thanks. It calls a non-static member function,
>without an instantiated object, with predictable defined results.
>Thereby showing that you don't need an object to call a (non-virtual)
>member function [You do need one for virtual member functions, for
>obvious reasons].


>i.e. It does what you have repeatedly claimed to be impossible.


>I'll explain it to you, now So pay attention


>/*
> class A is a POD-struct (no non-public data members, no virtual
>functions).
> This means that the address of any A object is the address of its
>first data member
> (an int) which means that I can type-pun between A* and int*
> i.e. if sizeof(int) = N, the first N bytes of an "A object", is the
>value representation of "num"
>*/
>class A {
>public:
> int num;
> int another_num;
> /*
> This getter function returns the value of the first data member,
> which for the reasons mentioned above is the same as *(int*)
>(address of object)
> */
> int f() { return num;}
> /*
> This setter function sets the first sizeof(N) bytes of the object
>to the value representation of x
> */
> void setnum(int x) {num = x;}
>};




>int main()
>{
> int z = 4;
> /* tell the compiler to pretend the object at &z, is an A, not an
>int */
> A* foo = reinterpret_cast<A*> (&z);


> /* Call the getter member function on our fictional A object. This
>is ok, because we only access the A->num data member, and we know
>thats precisely the storage occupied by z - which conveniently is the
>same type. So the first N bytes at that address are the integer value
>representation of 4 (the value of z)/ Since the methods aren't
>virtual, we don't need to access a vtable, or any type information,
>which is good, as there isn't any. We guarantee this compile-time
>dispatch because A is POD. That's why I had to have public data
>members */


Error 'f' : identifier not found.
What language is this code supposed to be ?.

> std::cout << f(foo) << std::endl;
> /* Call the setter member function on our fictional A object. This
>is also ok, for all the same reasons. We set the first N bytes at
>address foo = &z to the value representation of 7 */
> setnum(foo,7);
> /* And now, as if by magic, z is 7. The end. No A object was
>created by this code. I lied to the compiler that there was one at
>&z. I never allocated any storage, for example for the any data
>member A::another_num */
> std::cout << z << std::endl;
>}


>result: the code prints 4, then 7.




>See also, the side-by-side code at
>http://publib.boulder.ibm.com/infoce...ef/cplr035.htm


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
THIS WORKS - IT'S BEEN PROVEN! LET'S MAKE THIS HAPPEN! Arm7 C++ 1 08-28-2008 01:19 PM
The printing has been stopped and this job has been add to the queu? dejola Computer Support 6 12-30-2005 03:26 AM
I need help I has been over 6 months since I've been able to do the system check for updates Marc Computer Support 8 07-25-2005 07:04 PM
TRUE PROVEN METHOD OF HIGHER INTELLGENCE zetasum Java 2 02-28-2005 02:25 AM
PROVEN third party dropdown control needed David C ASP .Net 0 12-15-2004 05:19 PM



Advertisments