also an inheritance problem

D

David

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
 
V

Victor Bazarov

David said:
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
 
S

Salt_Peter

David said:
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
 
D

Dan Bloomquist

David said:
Hi all,
b *p1; ....
p1->setint(3);

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

Best, Dan.
 
M

melix

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...
 
J

Jim Langston

David said:
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 = new c();
p1->setint(3);
k=p2->getint();

delete p1;
delete p2;
 
A

Alf P. Steinbach

* VJ:
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/pointer-ref/50nullpointer.html
and I am sure there are more

Yes, the net is full of disinformation.
 
V

VJ

Alf said:
* VJ:



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.

I see you are using 0 instead of NULL, but which is more correct?

The way I see it, once they put the read nullPtr, it will be easier to do
#undef NULL
#define NULL nullPtr
instead of changing the whole code :p

Yes, the net is full of disinformation.
I have to agree there :(
 
?

=?iso-8859-1?q?Kirit_S=E6lensminde?=

David said:
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};
};


As others have pointed out your code doesn't compile so clearly isn't
the code that you've been running to get your output.

void 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

You have not initialised either p1 or p2 so both are probably just
pointing into random space. You must initialise them both.

The code implies that you have an incorrect mental model of how these
pointers should work. If you give us your explanation of what you are
expecting to do and how you think the code implements that expectation
then I think you will get a much more useful reply from the list.


K
 
A

Alf P. Steinbach

* VJ -> Alf P. Steinbach:
I see you are using 0 instead of NULL,
Where?


but which is more correct?

Both are technically correct.

The way I see it, once they put the read nullPtr, it will be easier to do
#undef NULL
#define NULL nullPtr
instead of changing the whole code :p

Apart from breaking the formal rules (§17.4.3.1/3), and apart from the
speling eror, this will break code that legitimately uses NULL as a zero
integral value.

But of course you may define your own NULLPTR macro, or whatever.
 
V

VJ

Alf said:
* VJ -> Alf P. Steinbach:



Where?


I should have said "I assume you are using 0 instead of NULL".
So, which one are you using?

Apart from breaking the formal rules (§17.4.3.1/3), and apart from the
speling eror, this will break code that legitimately uses NULL as a zero
integral value.

What spelling error? There might be, because English is not my native
language.

It will not break code which uses NULL only as a null pointer, and
nothing else. Besides, if NULL is integer zero, then this will compile
as well:

bool status = NULL;
int number = NULL;

but are there people using NULL in such way?
 
V

VJ

Alf said:
* VJ:



C++ is case-sensitive.

I know, but whats the error?


I did not say it is not.

This code is really representative:

int i2d_GENERAL_NAME(GENERAL_NAME *a, unsigned char **pp)
{
unsigned char *p;

/* Save the location of initial TAG */
if(pp) p = *pp;
else p = NULL;


What kind of project is this?!? They are comparing unsigned char** to false!

I bet they are ignoring milion warnings

Also this:
if (a == NULL) return;
Easiest way to make a stupid error like this:
if (a = NULL) return;
 
F

F.J.K.

VJ said:
I know, but whats the error?

Case-sensitive means an uppercase letter like 'P' is not the same as
the lowercase letter 'p'. So you have to take care, not to use the
wrong case in your source code. You are as sloppy in your example code
as you are in researching what is legal C++ and what is disinformation
on the web. This results in errors that you readily multiply over the
mailing list, resulting in even more disinformation on the web :-(
 
V

VJ

F.J.K. said:
VJ schrieb:




Case-sensitive means an uppercase letter like 'P' is not the same as
the lowercase letter 'p'. So you have to take care, not to use the

I said I know what is case-sensitive, but I did not know nullPtr is
actualy nullptr
wrong case in your source code. You are as sloppy in your example code
as you are in researching what is legal C++ and what is disinformation
on the web. This results in errors that you readily multiply over the
mailing list, resulting in even more disinformation on the web :-(

Do you have my example code to say I am sloppy?
I am not the one who assign integer numbers to a pointer, compare bool
variables to 0, compare integer to false, compare pointers to false, etc.

The page I found confirms what I said about using 0 as null pointer,
therefore someone else is spreading disinformation on the web :p

Also every normal c++ coding standard will tell you to use NULL(or
whatever there is), instead of 0


ps I am using NULL for null pointer, and have not seen nullptr yet
 
S

Salt_Peter

VJ said:
should be

A *ptr = NULL;

0 is not a null pointer

The pointer is not a literal 0, its an *address*.
Note that NULL means nothing until a library defines it.
Its exactly like using EXIT_SUCCESS or EXIT_FAILURE or any other
definition.
They don't exist until you or some library defines the macro. < ewww-
macros !?x?!>

If you see something like:

A* ptr = NULL;

You have no way of knowing what NULL is except through inspection. As
far as i'm concerned, that NULL could be anything. Thats why its rather
rare in C++ code. On the other hand:

A* ptr = 0;

Is explicitly a nullified pointer with a very clear intention.
And as recommended, in modern code, nullptr (not nullPtr) can safely be
defined as 0.
Perhaps i might suggest the following for reference and an interesting
read:
http://www.research.att.com/~bs/bs_faq2.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top