| |

VerySource

 Forgot password?
 Register
Search
View: 3635|Reply: 21

I feel that c ++ is a bad place

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-5 10:40:01
| Show all posts |Read mode
#include <iostream.h>

class A
{
public:
int v;
A () {}
virtual void f () {cout << "A :: f ()" << endl;}
virtual ~ A () {}
};

class B: public A
{
public:
B () {}
void f () {cout << "B :: f ()" << endl;}
~ B () {}
};

int main (int argc, char * argv [])
{
A * pa = new B ();
pa-> v = 1;
A c;
c.v = 2;

A&ra = * pa;
ra.f ();
ra = c;
ra.f ();

delete pa;
return 0;
}

The above program will output two lines of B :: f ()

Why not copy the virtual table too? If you don't know it, it is often debugged for a long time because of this mechanism.
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-5 19:54:01
| Show all posts
This has nothing to do with references. In C ++, if the object is copied by byte, the virtual function table pointer will not be copied in together. Unless hard to come: unsigned char * p = (unsigned char *) pa;
    unsigned char * q = (unsigned char *)&c;
for (int i = 0; i <sizeof (c); i ++)
    p [i] = q [i];
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-1-6 00:54:01
| Show all posts
Why not copy the virtual table too?
Since it is "copy", not just "pointing" or "referencing", what polymorphism is there?
You can't copy it as an object of class X and call a function of class Y at the same time.
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-6 02:03:01
| Show all posts
At first glance, the landlord has never studied software engineering.
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-1-6 06:03:01
| Show all posts
Oh, I didn't think about the landlord's problem clearly, and I talked about it.
The first floor is right, "object copy" does not affect virtual tables.
Ra was originally referencing a class B object, and this would not change.
ra = c;
This sentence just modifies the base class sub-object of that referenced object. You cannot modify the virtual table. Once you modify it, it means that it still refers to a class b object (), but it has a virtual table of class A. Then, the following sentence:
delete pa;
It will not work, and the destructor will not be called accurately. When the class is complicated, it is easy to get a memory leak.
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-6 07:30:01
| Show all posts
#include <stdlib.h>

int main (int argc, char * argv [])
{
A * pa = new B ();
pa-> v = 1;
A c;
c.v = 2;

A&ra = * pa;
ra.f ();

/ *
unsigned char * p = (unsigned char *) pa;
    unsigned char * q = (unsigned char *)&c;

for (int i = 0; i <sizeof (c); i ++)
p [i] = q [i];
* /
memcpy (pa,&c, sizeof (c));

ra.f ();

delete pa;
return 0;
}

I think this will probably please the landlord.

It really doesn't make any sense except for occasional entertainment. The class B object that was originally allocated is later copied into the class A object member. But this is also normal and can be used as some special initialization.
But later it is wrong, copy the virtual function table pointer into it, and the result is that the original class B object is converted to a class A object at runtime (a veritable runtime type conversion -_-!). So does ra refer to a class A object or a class B object?

A a;

B b;

The true meaning of b = a; is: copy the parent domain members of the parent object a to the child B object in bytes (assuming that the equal sign is not overloaded). If you also copy the virtual function pointer to b, it is equivalent to losing your qualification as class B, that is, assimilating with A.

Moderator Morning Star is right.
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 Invalid IP Address

Post time: 2020-1-6 08:54:01
| Show all posts
The semantics of assignments are relative to objects, while virtual function tables are relative to classes. Since the assignment only operates on the object, of course, the type conversion of the assigned object is not automatically performed (the compiler does not know the actual type of the referenced object during the assignment process). The type of a reference implies that the user manipulates the abstract form of the referenced object, but it does not (and should not) intervene in changing the type of the actual object.
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-1-6 17:00:01
| Show all posts
Ha ha, a topic worth thinking about, collect first!
Reply

Use magic Report

0

Threads

15

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-1-6 19:45:01
| Show all posts
The landlord is still not familiar with the mechanism of polymorphism.
Reply

Use magic Report

1

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-9 02:09:01
| Show all posts
Strange, this program does not report warnning even if you add the -Wall parameter on g ++ ???????

<< Thinking in C ++ >> vol.1 charpter 11

Once a reference is initialized to an object, it cannot be
changed to refer to another object. (Pointers can be pointed to
another object at any time.)

<< ISO C ++ 14882-2003 >> 8.5.3.2
A reference cannot be changed to refer to another object after initialization.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list