Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   also an inheritance problem (http://www.velocityreviews.com/forums/t458279-also-an-inheritance-problem.html)

David 11-09-2006 04:33 AM

also an inheritance problem
 
Hi all,

I am really new in C++. I met a problem. Could someone help me?

I have a base class:
class a
{
protected:
int b;
public:
int getint(){return b;};
setint (int a){ b=a};
};
class b:public a
{
.......
}
class c:public a
{
}

main
{
int k;
b *p1;
c *p2;
p1->setint(3);
k=p2->getint();
}

why the value of k is not 3? please help me. how can do that. thanks


Victor Bazarov 11-09-2006 04:44 AM

Re: also an inheritance problem
 
David wrote:
> Hi all,
>
> I am really new in C++. I met a problem. Could someone help me?
>
> I have a base class:
> class a
> {
> protected:
> int b;
> public:
> int getint(){return b;};
> setint (int a){ b=a};
> };
> class b:public a
> {
> ......
> }
> class c:public a
> {
> }
>
> main
> {
> int k;
> b *p1;
> c *p2;
> p1->setint(3);
> k=p2->getint();
> }
>
> why the value of k is not 3? please help me. how can do that. thanks


Even if we remove all syntax errors, you still have a very bad problem
there: both p1 and p2 pointers do not actually point to anything.
Trying to call a member function through a pointer that doesn't point
to a real (valid) object has undefined behaviour.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



Salt_Peter 11-09-2006 05:04 AM

Re: also an inheritance problem
 

David wrote:
> Hi all,
>
> I am really new in C++. I met a problem. Could someone help me?
>
> I have a base class:
> class a
> {
> protected:
> int b;
> public:
> int getint(){return b;};
> setint (int a){ b=a};
> };
> class b:public a
> {
> ......
> }
> class c:public a
> {
> }
>
> main
> {
> int k;
> b *p1;
> c *p2;
> p1->setint(3);
> k=p2->getint();
> }
>
> why the value of k is not 3? please help me. how can do that. thanks


Pointers are not objects. A pointer is nothing more than an address
that points to nothing.
The mailman will not construct a home because you sent a letter to a
fictitious address.
A car's license plate does not belong to a car until you put it on the
car.

#include <iostream>

class A
{
int a;
public:
A() : a(0) { } // default constructor
int get() const { return a; }
void set(int n) { a = n; }
};

int main()
{
A instance; // an object
std::cout << "&instance = " << &instance << std::endl;
A* p_a; // a pointer to nothing
p_a = 0; // a null pointer
std::cout << "pointer p_a = " << p_a << std::endl;
p_a = &instance; // p_a now actually points to something real
std::cout << "pointer p_a = " << p_a << std::endl;
p_a->set( 3 );
std::cout << "p_a->get() = " << p_a->get() << std::endl;
int k = p_a->get();
std::cout << "k = " << k << std::endl;
}

/*
&instance = 0x7fff42d853c0
pointer p_a = 0
pointer p_a = 0x7fff42d853c0
p_a->get() = 3
k = 3
*/

Whenever you declare a pointer, remember to always initialize it.

A a;
A* ptr = 0; // null pointer
ptr = &a; // a valid pointer to an object


Dan Bloomquist 11-09-2006 05:31 AM

Re: also an inheritance problem
 


David wrote:

> Hi all,
> b *p1;

....
> p1->setint(3);


A reasonable compiler would have warned the uninitialized pointer. Have
you compiled this code?

Best, Dan.


melix 11-09-2006 07:48 AM

Re: also an inheritance problem
 
If you wanto to get same number whenever you called getInt function you
should declare int b as static.

And Its very right, dont forget to initialize the pointer. Because NULL
pointers has always been a trouble...

On Nov 9, 7:31 am, Dan Bloomquist <publi...@lakeweb.com> wrote:
> David wrote:
> > Hi all,
> > b *p1;

> ...
> > p1->setint(3);A reasonable compiler would have warned the uninitialized pointer. Have

> you compiled this code?
>
> Best, Dan.



VJ 11-09-2006 10:07 AM

Re: also an inheritance problem
 
Salt_Peter wrote:

> A* ptr = 0; // null pointer


should be

A *ptr = NULL;

0 is not a null pointer

Alf P. Steinbach 11-09-2006 10:34 AM

Re: also an inheritance problem
 
* VJ:
> Salt_Peter wrote:
>
>> A* ptr = 0; // null pointer

>
> should be
>
> A *ptr = NULL;
>
> 0 is not a null pointer


Please only correct when the correction is correct.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

VJ 11-09-2006 11:12 AM

Re: also an inheritance problem
 
Alf P. Steinbach wrote:
> * VJ:
>
>> Salt_Peter wrote:
>>
>>> A* ptr = 0; // null pointer

>>
>>
>> should be
>>
>> A *ptr = NULL;
>>
>> 0 is not a null pointer

>
>
> Please only correct when the correction is correct.
>


Isn't the correction correct?

The 1st googled page about c++ NULL pointer gives:
http://www.fredosaurus.com/notes-cpp...llpointer.html
and I am sure there are more

Same goes for
bool check = 0;
and similar definitions

Jim Langston 11-09-2006 11:35 AM

Re: also an inheritance problem
 

"David" <clamayi@gmail.com> wrote in message
news:1163046836.515900.35500@f16g2000cwb.googlegro ups.com...
> Hi all,
>
> I am really new in C++. I met a problem. Could someone help me?
>
> I have a base class:
> class a
> {
> protected:
> int b;
> public:
> int getint(){return b;};
> setint (int a){ b=a};
> };
> class b:public a
> {
> ......
> }
> class c:public a
> {
> }
>
> main
> {
> int k;
> b *p1;


b* p1 = new b();

> c *p2;


c* p2 = new c();

> p1->setint(3);
> k=p2->getint();


delete p1;
delete p2;

> }
>
> why the value of k is not 3? please help me. how can do that. thanks
>




Alf P. Steinbach 11-09-2006 11:52 AM

Re: also an inheritance problem
 
* VJ:
> Alf P. Steinbach wrote:
>> * VJ:
>>
>>> Salt_Peter wrote:
>>>
>>>> A* ptr = 0; // null pointer
>>>
>>>
>>> should be
>>>
>>> A *ptr = NULL;
>>>
>>> 0 is not a null pointer

>>
>>
>> Please only correct when the correction is correct.
>>

>
> Isn't the correction correct?


Nope.

It's correct that literal 0 is not itself a null-pointer. It's
incorrect that the macro NULL is a null-pointer: NULL is required to be
defined as a zero constant integral rvalue, e.g. as 0 or 0L. Both
designate the null-pointer value of type T* when used in a context where
a T* is expected, such as the initializations above, and both designate
simply a zero integral value in other contexts.

Reason etc. In C, as opposed to C++, NULL can be defined as a pointer
value, e.g. as (void*)0, which can help good compilers and lint tools to
emit meaningful diagnostics. In C++ void* is not assignment compatible
with other pointer types, so (void*)0 is not a practical null-pointer
value in C++ -- you'd need casts peppered all over the code. In C++0x
it seems we'll get a special keyword nullptr. MSVC already uses that
keyword for .NET code, and IIRC it's been mentioned that g++ implements
that keyword, although the 3.4.4 compiler doesn't recognize it, and
although I'm unable to find any reference at the moment. It's possible
to define something similar, but part of the rationale for the keyword
is that such library solutions lead to gobbledegook error diagnostics.


> The 1st googled page about c++ NULL pointer gives:
> http://www.fredosaurus.com/notes-cpp...llpointer.html
> and I am sure there are more


Yes, the net is full of disinformation.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


All times are GMT. The time now is 07:16 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.