newbie: pointer to a class ?

V

verbatime

Okay im trying to do a simple thing, i have a class:

class cBasic{

};

And i have an instance of it here:

cBasic Basic;

I have a pointer:

cBasic *pBasic;

Now i would like to point pBasic to Basic.

What i thought, was that i do this normally, like ex:

pBasic = &Basic;

But that doesnt point to the right direction, when i try to inspect through
pBasic.

So simple question - how do i make a pointer point on the class - so by
doing pBasic-> i can access the data just as i would do with Basic. ?

Thanx.
 
L

Leor Zolman

Okay im trying to do a simple thing, i have a class:

class cBasic{

};
ok


And i have an instance of it here:

cBasic Basic;

yes you sure do.
I have a pointer:

cBasic *pBasic;
yup.


Now i would like to point pBasic to Basic.

What i thought, was that i do this normally, like ex:

pBasic = &Basic;

That's actually just the way you do it.
But that doesnt point to the right direction, when i try to inspect through
pBasic.

Aha. You haven't shown us /that/ code ;-)

Here's your code, with a few minimal things thrown in so we can really
test it:

#include <iostream>
using namespace std;

class cBasic{
public:
int i; // I just added this
};

int main()
{
cBasic Basic;
cBasic *pBasic;

pBasic = &Basic;

pBasic -> i = 100;
cout << pBasic -> i << endl;
return 0;
}

This prints 100.

Now, in terms of real class design, there's all sorts of things here "one
shouldn't do"; for example, you'd use a constructor rather than manually
stuffing that 100 into the object. But hopefully this'll get you over the
hump with the pointer to object issue (not really "pointer to a class").

Good luck,
-leor



So simple question - how do i make a pointer point on the class - so by
doing pBasic-> i can access the data just as i would do with Basic. ?

Thanx.

Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
R

Russell Hanneken

verbatime said:
i have a class:

class cBasic{

};

And i have an instance of it here:

cBasic Basic;

I have a pointer:

cBasic *pBasic;

Now i would like to point pBasic to Basic.
What i thought, was that i do this normally, like ex:

pBasic = &Basic;

But that doesnt point to the right direction, when i try to inspect through
pBasic.
So simple question - how do i make a pointer point on the class - so by
doing pBasic-> i can access the data just as i would do with Basic. ?

What you did should work. (I assume your actual cBasic class has some
members.) Are you getting compiler errors, or is your program just
doing something unexpected? Post a small code sample that exhibits your
problem, and we'll see what's going on.

Regards,

Russell Hanneken
(e-mail address removed)
Remove the 'g' from my address to send me mail.
 
L

Leor Zolman

int main()
{
cBasic Basic;
cBasic *pBasic;

pBasic = &Basic;

pBasic -> i = 100;

Sorry, just realized that this wasn't "really testing it". What I meant to
say in the line above was:

Basic.i = 100;
cout << pBasic -> i << endl;
return 0;
}

That way, you can actually tell that the value written into the object is
later being accessed through the pointer (long day).
-leor


Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
V

verbatime

Leor Zolman said:
Aha. You haven't shown us /that/ code ;-)

Hi leon ;)

Thanx....well youre right i didnt post my code ;) - and ofcourse thats where
the problem lies, and suddenly it became a little bit more complicated than
i wanted it to be - anyway, doesnt seems to be anything about basic
pointers.

I have a baseclass:

class bcBasic
{

}

And the i have an inherated class:

class cBasic : public bcBasic
{

}

What i tried to do, was to pass on a pointer to cBasic - to a function that
wants a bcBasic pointer - and i guess that makes sense somehow :)

Okay now for my next simple question; the reason why i have the base class
is that im using it from a DLL - and everything in that class defination is
pure virtual. What i thought was that doing a "class cBasic : public
bcBasic" - with the exact same structure as the baseclass, that there
wouldnt be any difference....well there was.

So my simple question: how on earth do i build up a class-skeleton, so my
DLL knows what to expect, without knowing the class in front ?

That would mean that it was possible to define a skeleton-class, and then
have alot of instances that is of this class type - BUT with different code
in each of them (without changing which class it is)....

If its not clear - im doing a callback from a DLL to a mainapplication - so
the DLL is able to use a class inside the mainapplication.

Really hope you can help - somehow i think that this must be very simple
C++ - but im not sure.....
 
A

Andrey Tarasevich

verbatime said:
Okay im trying to do a simple thing, i have a class:

class cBasic{

};

And i have an instance of it here:

cBasic Basic;

I have a pointer:

cBasic *pBasic;

Now i would like to point pBasic to Basic.

What i thought, was that i do this normally, like ex:

pBasic = &Basic;

But that doesnt point to the right direction, when i try to inspect through
pBasic.

That actually does "point to the right direction". You must've
misinterpreted the results of did something differently.
 
V

verbatime

SORRY ALL....


Sometimes you just have to go to sleep when simple things just doesnt
work....

The passing of pointers of a "xxxx : public yyyy" is working ok i think - my
bad was that i was a little bit too fast on the copy & paste - and copied
the variables from my skeleton (base) into the derived class.

Which gave somekind of conflict since the names where the same....

Sorry for spending your time - its working now.
 
M

Mike Wahler

verbatime said:
SORRY ALL....


Sometimes you just have to go to sleep when simple things just doesnt
work....

The passing of pointers of a "xxxx : public yyyy" is working ok i think - my
bad was that i was a little bit too fast on the copy & paste - and copied
the variables from my skeleton (base) into the derived class.

Which gave somekind of conflict since the names where the same....

Sorry for spending your time - its working now.

All too common novice error: Being in a hurry, trying
to move too fast. Baby steps. Programming is 90%
thinking, 10% coding/testing/debugging. Or something
like that.

-Mike
 
H

Howard

Mike Wahler said:
All too common novice error: Being in a hurry, trying
to move too fast. Baby steps. Programming is 90%
thinking, 10% coding/testing/debugging. Or something
like that.

From what I've seen, programming is indeed 90% thinking and 10% coding. Of
course, many programmers do that 90% thinking after doing half the coding
and realizing it's simply not working. :) And then, of course, you've got
another 50% adding new features demanded by management. Then another 20%
explaining to them why the program does what thay requested and not what
they really wanted, and then another 75% fixing the bugs that a month of
testing missed but your users found (kindly) for you, then another 50%
updating everything because the apps and libraries you interact with just
updated their versions, then another 250% designing a whole new version
because Windows added a new operating system, etc... Some say it's a pain.
I call it job security. :)

-Howard
 

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

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top