1

Topic: Constant copy of a class. Who as does?

Let's admit, there is a certain class and depending on, whether the copy const or non-const at this copy is or there is no implementation , or it is. Who as solves the similar task?

2

Re: Constant copy of a class. Who as does?

rdb_dev wrote:

Who as solves the similar task?

it is what that the strange task))

3

Re: Constant copy of a class. Who as does?

egorych, there is a necessity to change behavior of a copy of a class when it const but if normal methods can be made const and they will be caused only for const the pointer/reference on object designers and  to make const, dividing thus behavior at creation/destruction, it is impossible.
Here I also ask - who and how solved the similar task?

4

Re: Constant copy of a class. Who as does?

rdb_dev wrote:

egorych, there is a necessity to change behavior of a copy of a class when it const but if normal methods can be made const and they will be caused only for const the pointer/reference on object designers and  to make const, dividing thus behavior at creation/destruction, it is impossible.
Here I also ask - who and how solved the similar task?

It where to the such learn?

5

Re: Constant copy of a class. Who as does?

rdb_dev wrote:

It is admissible, there is a certain class and depending on, whether the copy const or non-const at this copy is or there is no implementation , or it is. Who as solves the similar task?

Return directed by the real task, instead of to  to representation as it to do.

6

Re: Constant copy of a class. Who as does?

rdb_dev wrote:

egorych, there is a necessity to change behavior of a copy of a class when it const but if normal methods can be made const and they will be caused only for const the pointer/reference on object designers and  to make const, dividing thus behavior at creation/destruction, it is impossible.
Here I also ask - who and how solved the similar task?

Well as a variant, it is possible  in object if at least time was caused in it not constant  it not constant and already in  to make the decision on the basis of this flag. But it badly smells, if is fair.

7

Re: Constant copy of a class. Who as does?

rdb_dev wrote:

designers and  to make const, dividing thus behavior at creation/destruction, it is impossible

and it is correct, for the class with the constant designer will not manage to be created, and with constant  - to destroy)))
PS look in stl, there made separate classes for such perverted cases, iterator and const_iterator as an example

8

Re: Constant copy of a class. Who as does?

If I correctly understood all, designers are caused for constant and not constant objects. A difference only in invocation point.

9

Re: Constant copy of a class. Who as does?

egorych wrote:

and it is correct, for the class with the constant designer will not manage to be created, and with constant  - to destroy)))
PS look in stl, there made separate classes for such perverted cases, iterator and const_iterator as an example

That's just the point that at initialization of constant object it can be initialized constant values without their copying in a heap, i.e. refer to them, and at corrupting, naturally, should not try to clear these resources. It would not be desirable to check in  at all, where exactly there is a resource - in a heap or in resources of the binary unit. Most likely, it is necessary to get out somehow through the template specification.

10

Re: Constant copy of a class. Who as does?

CEMb wrote:

If I correctly understood all, designers are caused for constant and not constant objects. A difference only in invocation point.

And the invocation point at them can be same. A difference only that by a call of a constant method to it arrives constant this with all that it implies.

11

Re: Constant copy of a class. Who as does?

rdb_dev wrote:

And the invocation point at them can be same. A difference only that by a call of a constant method to it arrives constant this with all that it implies.

By a call to it the pointer arrives and nobody knows what it.
the Keyword const specifies that variable value is to constants and informs the compiler that the programmer should not change it.

12

Re: Constant copy of a class. Who as does?

blonduser, what hinders to check up?

class A {
volatile int a;
public:
void set (int src) const
{
int * p_a = reinterpret_cast <int *> (reinterpret_cast <void *> (this) ++);
//int * p_a = reinterpret_cast <int *> ((size_t) this + sizeof (void *));
*p_a = src;
}
};

Result of compilation:

 main.cpp: In member function ' void A:: set (int) const ':
main.cpp:38:70: error: reinterpret_cast from type ' const A * ' to type ' void * ' casts away qualifiers
int * p_a = reinterpret_cast <int *> (reinterpret_cast <void *> (this) ++); 

13

Re: Constant copy of a class. Who as does?

rdb_dev;
With functions Set const it is not used normally. But you are a good judge in perversions!
It happens only at the moment of compilation.
My variant works.

class A {
volatile int a;
public:
void set (int src) const
{
int * pA = (int *) (void *) &a;
*pA = src;
//int * p_a = reinterpret_cast <int *> (reinterpret_cast <void *> (this) ++);
//int * p_a = reinterpret_cast <int *> ((size_t) this + sizeof (void *));
/*p_a = src;
}
};

14

Re: Constant copy of a class. Who as does?

blonduser wrote:

It happens only at the moment of compilation.

it is natural! Where still? smile
The compiled binary unit does not store the information on qualifiers. You understand that access to any instance member of a class is carried out under the implicit pointer this which it is possible to specify, and it is possible and not to specify in method implementations, and access under the constant pointer, it is access read-only.
P.S. By the way, try in my example a line with an error to replace for the line from the comment. wink

15

Re: Constant copy of a class. Who as does?

rdb_dev;
So you that want to receive as a result?

16

Re: Constant copy of a class. Who as does?

blonduser wrote:

rdb_dev so you that you want to receive as a result?

About it I already wrote all above, and now we left in small  on arguing of singularities of constant methods. If it will be possible to make beautiful implementation on the specification of a template with deduction, I will lay out.

17

Re: Constant copy of a class. Who as does?

rdb_dev wrote:

It is admissible, there is a certain class and depending on, whether the copy const or non-const at this copy is or there is no implementation , or it is. Who as solves the similar task?

You are wrong,  at a class is always, and it is always caused.
Implementation thus same.

18

Re: Constant copy of a class. Who as does?

rdb_dev wrote:

egorych, there is a necessity to change behavior of a copy of a class when it const but if normal methods can be made const and they will be caused only for const the pointer/reference on object designers and  to make const, dividing thus behavior at creation/destruction, it is impossible.
Here I also ask - who and how solved the similar task?

You can declare two designers for const and not const objects, and to cause them accordingly.
To distinguish designers from each other it is necessary hands (it is possible to make two successors that it was visible in a class title).
Thus you can initialize in the designer a certain flag in a class to distinguish, whether is given
const or is not.
Once again, thus to differentiate objects on const| const to you it is necessary hands. For simplification it is possible to hide still
Creation of copies in factory.
P.S. Yes, the task idiotic, I agree with all who so expressed. All against an object model With ++.

19

Re: Constant copy of a class. Who as does?

MasterZiv wrote:

it is passed...
You are wrong,  at a class is always, and it is always caused.
Implementation thus same.

I know that  is always, therefore and wrote not "was not present ", and "there is no implementation ", meaning ~T () {}

20

Re: Constant copy of a class. Who as does?

MasterZiv, do not distract it. Very much it would be desirable to look at result.

21

Re: Constant copy of a class. Who as does?

MasterZiv wrote:

...
To distinguish designers from each other it is necessary hands (it is possible to make two successors that it was visible in a class title).
...
For simplification it is possible to hide still creation of copies in factory.

In this case, the problem is not present generally.
The base class, two successors: TMyClassInHeap, TMyClassAsRef
And it is simple virtual

22

Re: Constant copy of a class. Who as does?

blonduser, you will not wait! smile))
With deduction it is impossible, and without it quits banal exactly specialized template which even the schoolboy writes.

23

Re: Constant copy of a class. Who as does?

rdb_dev wrote:

the Difference only that by a call of a constant method to it arrives constant this with all that it implies.

It is a difference - for the compiler . The code does not distinguish const. I think, the compiler at generation distinguishes ctor/dtor from remaining methods. I.e. you cannot call it explicitly (will not be compiled).

24

Re: Constant copy of a class. Who as does?

CEMb wrote:

It is a difference - for the compiler . The code does not distinguish const. I think, the compiler at generation distinguishes ctor/dtor from remaining methods. I.e. you cannot call it explicitly (will not be compiled).

I and wrote:

25

Re: Constant copy of a class. Who as does?

MasterZiv wrote:

You can declare two designers for const and not const objects, and to cause them accordingly.
To distinguish designers from each other it is necessary hands (it is possible to make two successors that it was visible in a class title).
Thus you can initialize in the designer a certain flag in a class to distinguish, whether is given
const or is not.
Once again, thus to differentiate objects on const| const to you it is necessary hands. For simplification it is possible to hide still
Creation of copies in factory.
P.S. Yes, the task idiotic, I agree with all who so expressed. All against an object model With ++.

Nea declares const the designer cannot! Designers and  cannot have cv qualifiers.
As a result it turns out precisely specialized two templates of a class-wrapper (for const and not const) to an interesting class, plus typedef' for them. Here so - it is banal and it is boring. There is no possibility  a template under the declaration lvalue.

template <class T> class A _;
template <>
class A _ <A const>: public A
{
...
};
template <>
class A _ <A>: public A
{
...
};
typedef A _ <const A> ClassConstA;
typedef A _ <A> ClassA;

Generally, in a C ++ it is a lot of "innuendo". For example:
* There is a possibility to "tell" new const Type (), but there is no possibility this const though somehow to "hear" in implementation of the overloaded operator new, the designer or  a class;
* There is a possibility to redefine placement new, but the compiler does not feign its call at determination of a copy of object on a stack though, such behavior would be logical;
* We can  templates with deduction on parameters of the designer, but we can not  templates with deduction on type and declaration qualifiers lvalue;