|
-
February 20th, 2010, 08:10 PM
#1
[c++] typedef class
If i have the following class:
Code:
class cBlaat
{
public:
void SomeFunc();
};
typedef class cBlaat * LPBLAAT;
Then what does that last line mean?? What could be the reason for this..??
"typedef class cBlaat * LPBLAAT"
-
February 21st, 2010, 01:58 AM
#2
Re: [c++] typedef class
It just makes LPBLAAT an alias for the type cBlaat*. The class keyword in the typedef is unnecessary.
-
February 21st, 2010, 02:52 AM
#3
-
February 21st, 2010, 05:04 AM
#4
Re: [c++] typedef class
Ok thanks, then i have another question about a class. Suppose i have the following code:
Code:
class cBlaat
{
public:
DoSomething();
};
cBlaat * blaat; //doesn't work
int main()
{
blaat->DoSomething();
return 0;
}
Why doesn't "cBlaat * blaat" work when i declare it outside of a function, but does it work when i declare it as "cBlaat blaat" ??
I just don't get what's so different about it...
-
February 21st, 2010, 06:17 AM
#5
Re: [c++] typedef class
 Originally Posted by vivendi
Why doesn't "cBlaat * blaat" work when i declare it outside of a function, but does it work when i declare it as "cBlaat blaat" ??
It works, but looking at your sample code (which will not even compile), you probably tried to dereference the pointer when it did not point to an object (in this case, it is a null pointer).
-
February 21st, 2010, 09:21 AM
#6
Re: [c++] typedef class
 Originally Posted by vivendi
Why doesn't "cBlaat * blaat" work when i declare it outside of a function, but does it work when i declare it as "cBlaat blaat" ??
I just don't get what's so different about it...
To expand on what laserlight said...
The firstonly declares a pointer to a cBlaat object - it does not create an instance of the object - you only have an invalid pointer at this point.
The secondactually creates an instance of the cBlaat object - so you actually have one of those objects existing and can operate on it.
Hope that helps.
Be sure to rate those who help!
-------------------------------------------------------------
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
February 21st, 2010, 09:46 AM
#7
Re: [c++] typedef class
 Originally Posted by laserlight
It works, but looking at your sample code (which will not even compile), you probably tried to dereference the pointer when it did not point to an object (in this case, it is a null pointer).
No it's not, it's an undefined pointer, unless my terminology of what a null pointer is is wrong. I think a null pointer is this:
Code:
cBlaat * blaat = NULL;
-
February 21st, 2010, 09:59 AM
#8
Re: [c++] typedef class
 Originally Posted by ninja9578
No it's not, it's an undefined pointer, unless my terminology of what a null pointer is is wrong.
Notice that it has static storage duration, hence it is zero initialised.
-
February 22nd, 2010, 12:46 AM
#9
Re: [c++] typedef class
 Originally Posted by laserlight
Notice that it has static storage duration, hence it is zero initialised.
Yes, but I think it should compile (at least after looking at it, I don't see any compilation error). Runtime error? yes.
-
February 22nd, 2010, 08:09 AM
#10
Re: [c++] typedef class
 Originally Posted by rohshall
Yes, but I think it should compile (at least after looking at it, I don't see any compilation error).
What's the return type of cBlaat::DoSomething()?
-
February 22nd, 2010, 09:05 AM
#11
Re: [c++] typedef class
The function is also used, but never defined.
Remember that
Code:
class foo {
void DoSomething(void);
};
void foo::DoSomething(void){
std::cout << "Hello World" << std::endl;
}
foo * bar;
main(){
bar -> DoSomething();
}
Is undefined behavior, but stable on almost every compiler.
Also, why do statically declared pointers initialize to zero? Seems like a waste of a mov command to me. I always do this
If I want to know if the pointer has been initialized or not later. If I omit the zero, I don't care what it is.
Last edited by ninja9578; February 22nd, 2010 at 09:07 AM.
-
February 22nd, 2010, 12:57 PM
#12
Re: [c++] typedef class
 Originally Posted by ninja9578
Also, why do statically declared pointers initialize to zero? Seems like a waste of a mov command to me.
If it were not initialized to 0, then the compiler is broken. The rule that globals and statics are initialized to 0 goes back to 'C' programming.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|