Ugly cast
-
Yes, i know how virtual baseclasses work, what i was wondering was about casting to a subclass with no data from a baseclass. toxcct wrote: ps: why do you put your int i; into public statement ? I did it so a could access it from B and i dont like the protected keyword..=) /Magnus
- I don't necessarily agree with everything I say
What you are doing will work then, but it is really bad code. Sometimes people do what you do when they have no choice and need to access protected and private members in a given class. This is usually do to poor design of the base class. (*cough* BORLAND VCL) However, what you are doing is very questionable indeed. It looks like a maintenance nightmare. Tim Smith I'm going to patent thought. I have yet to see any prior art.
-
oh :wtf: you should ;P because there is no protection on your i member, and so anybody can modify it ... you agree so that there is no interrest ;)
TOXCCT >>> GEII power
-
There are many cases where a member can be public without issue. Look at the CRect class. What would be the point of protecting left, top, right and bottom? Tim Smith I'm going to patent thought. I have yet to see any prior art.
-
It works but it is very very bad (and wrong). What if B had another variable J and a routine IncJ? If you invoked IncJ, you would be trashing memory and could cause your program to crash. Tim Smith I'm going to patent thought. I have yet to see any prior art.
-
oh :wtf: you should ;P because there is no protection on your i member, and so anybody can modify it ... you agree so that there is no interrest ;)
TOXCCT >>> GEII power
I dont really see the point with the protected keyword. Maybe someone inherits from the class and then it can be modified, either it is publicly modifiable or not, i dont see the point with have it "maybemodifable". /Magnus
- I don't necessarily agree with everything I say
-
I dont really see the point with the protected keyword. Maybe someone inherits from the class and then it can be modified, either it is publicly modifiable or not, i dont see the point with have it "maybemodifable". /Magnus
- I don't necessarily agree with everything I say
-
no, a protected member is only modifiable by the derived classes objects (and of course objects from the class itself), and occasionately by friends functions too, but in any case by the outside.
TOXCCT >>> GEII power
-
Yes i know how it works i just dont see the point with the protected keyword. /Magnus
- I don't necessarily agree with everything I say
-
class A { public: A() : i(0) { } int i; }; class B : public A { public: void Inc() { i++; } }; A *pa = new A; B *pb = (B *)pa; pb->Inc();
The pointer is not a type B, but class B has no own data and if i run it it works. Is this valid? What happens when i do that? /Magnus
- I don't necessarily agree with everything I say
IT may work for your particular sutiation... but the proper way of casting in this sense is with dynamic_cast:
A *pa = new A; B *pb = dynamic_cast<B *>(pa);
pb will either contain a valid pointer to a B object, or will be NULL if it can't be casted safely. Sometimes I feel like I'm a USB printer in a parallel universe. -
class A { public: A() : i(0) { } int i; }; class B : public A { public: void Inc() { i++; } }; A *pa = new A; B *pb = (B *)pa; pb->Inc();
The pointer is not a type B, but class B has no own data and if i run it it works. Is this valid? What happens when i do that? /Magnus
- I don't necessarily agree with everything I say
It works by accident. It's why you should never use the old style C cast. This compiles,
B * pb = (B*)0;
pb->Inc () ;but goes bang. Paul
-
It works by accident. It's why you should never use the old style C cast. This compiles,
B * pb = (B*)0;
pb->Inc () ;but goes bang. Paul
-
I am not saying you shouldn't, I am saying you don't have to. This isn't a black/white issue. It isn't about always doing it or never doing it. It is about doing it when it is appropriate. Tim Smith I'm going to patent thought. I have yet to see any prior art.
-
I dont really see the point with the protected keyword. Maybe someone inherits from the class and then it can be modified, either it is publicly modifiable or not, i dont see the point with have it "maybemodifable". /Magnus
- I don't necessarily agree with everything I say
The protected keyword is for people who trust others to properly augment the implementation of a class in a derived class. In the last three or four weeks I have wasted endless amounts of time because VCL (Borland's MFC) made far too many things private. I couldn't fix the bugs in their code without completely replacing their classes. If they had made their stuff protected and trusted me, I would have saved a lot of time. I hardly ever use the private keyword because I trust the programmers not to screw up the implantation. If they do screw things up, I beat them up and then make them fix it. I would rather them do that than have to reimplement my classes in their own stuff creating two implementations of the same theme. Tim Smith I'm going to patent thought. I have yet to see any prior art.
-
Hmm B * pc = reinterpret_cast < B *> (0); This compiles too, so does that mean you should never use it either? Tim Smith I'm going to patent thought. I have yet to see any prior art.
reinterpret_cast
stands out like a sore thumb whereas the C style one doesn't. In either case you can blow your foot off by demanding the compilation of something absurd, like the original poster. Paul -
I dont really see the point with the protected keyword. Maybe someone inherits from the class and then it can be modified, either it is publicly modifiable or not, i dont see the point with have it "maybemodifable". /Magnus
- I don't necessarily agree with everything I say
The
protected
keyword lets you differentiate between accesses of the value by users of the class from accesses by derived classes. Ordinary users of the class may not access a member markedprotected
. A derived class, which supplements or modifies the behavior of the base class, is granted privileged access.
Software Zen:
delete this;