Archived

This topic is now archived and is closed to further replies.

HappyToLearnC

3-basic C++ questions

Recommended Posts

This from the d3dapp.cpp in the DX8.1 SDK common file //The programmer declares a class object from within the class. Why? static CD3DApplication* pd3dApp; //Why he want to cast the object to lParam. What does it mean. pd3dApp = (CD3DApplication*)lParam; //Why he can not refer to a class member variale without a class pointer? pd3dApp->m_dwAdapter = dwNewAdapter;

Share this post


Link to post
Share on other sites
I haven't used DX8.1 but I'll try to give generic answers..
quote:
Original post by HappyToLearnC
//The programmer declares a class object from within the class. Why?
static CD3DApplication* pd3dApp;

He doesn't declare a class there. He makes a (static) pointer to another class. He does this so that he can use some other object's functionality from this object/class.
quote:
//Why he want to cast the object to lParam. What does it mean.
pd3dApp = (CD3DApplication*)lParam;

He casts lParam to CD3DApplication*. lParam is probably some pointer that can point to anything. But here the programmer apparently knows that lParam points to CD3DApplication-type object, so he explicitely casts the object to that type of pointer. He can't just write pd3dApp = lParam, because lParam is unknown/wrong type.
quote:
//Why he can not refer to a class member variale without a class pointer?
pd3dApp->m_dwAdapter = dwNewAdapter;

Because m_dwAdapter is a member of some other class than this. You can only refer to this class' members without using pointers/other referring mechanisms. If you could refer to any classes member variable without using a pointer, then the code would be really messy and you could only have one object of each class.

I suggest you first read some introduction to classes/C++ instead of trying to fiqure out what some D3D applications do.

[edited by - civguy on September 3, 2002 6:14:46 AM]

Share this post


Link to post
Share on other sites
Thanks for the reply

quoting civguy
quote:

He doesn''t declare a class there. He makes a (static) pointer to another class. He does this so that he can use some other object''s functionality from this object/class.


Here is the method that includes the three lines of code in my question

INT_PTR CALLBACK CD3DApplication::SelectDeviceProc( HWND hDlg, UINT msg,WPARAM wParam, LPARAM lParam )

As you can see the class name is CD3DApplication, and the pointer he is creating inside this method (SelectDeviceProc) is of type CD3DApplication

Share this post


Link to post
Share on other sites
Ah. That's because one class can have several instances (called objects). In different instances, the values are different but the member functions are same. I'll give a brief (and stupid) example


      
#include <iostream>

class MyClass {
int _number;
MyClass* _pointer;

public:
MyClass(int number) : _number(number), _pointer(0) {}
MyClass(int number, MyClass* pointer) : _number(number), _pointer(pointer) {}

void printNumber() {
cout << _number << endl;
}

void printPointersNumber() {
//eek _pointer can be 0, but I don't care now

cout << _pointer->number << endl;
}
};

int main() {
MyClass class1(5);
//now class1._number = 5 and class1._pointer = 0


MyClass class2(10, &class1);
//now class2._number = 10 and class2._pointer = &class1


class1.printNumber(); //prints out 5

class2.printNumber(); //prints out 10

class2.printPointersNumber(); //prints out 5 (!). Because class2 has a pointer to class1, which has 5 as _number

return 0;
}

I hope this cleared it a bit at least. The pointers can point to different objects even though they are of same type.

This type of "pointing to an object of same type" can be very handy in linked lists for example. In linked list, each object points to 'next' object of the same type, forming a chain of objects

object1 points to object2, which points to object3 which points to object4.. Now you can begin at object1 and follow the pointers to next objects, thus reading the whole chain (aka. list).

[edited by - civguy on September 3, 2002 7:36:00 AM]

Share this post


Link to post
Share on other sites
Thanks for the time.

I just was not sure why he did not use the "this" keyword instead of creating a pointer, I guess the reason he is pointing to a static members. Because if he did it will create an instance everytime that part of the code executes. But the fact remains that I am still not sure.

Thanks

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
if you are asking these kinds of questions you probably should hold off on DirectX. Nothing to be ashamed about text games.

Share this post


Link to post
Share on other sites
None of your questions have anything to do with DirectX.

//The programmer declares a class object from within the class. Why?
static CD3DApplication* pd3dApp;

It''s one way of implementing what is called a singleton - a class that allows only one instance (object) of its type to be declared. In this method, the instance is a private class variable (class variables belong to the whole class, not just one object), and code elsewhere will make sure it exists and is not redefined until the application object is terminated.

//Why he want to cast the object to lParam. What does it mean.
pd3dApp = (CD3DApplication*)lParam;

The object is being cast from lParam, not to it. I suspect this is in some sort of static WndProc-like method and allows the CD3DApplication object to be associated with some legacy/Win32 structure such as an HWND.

//Why he can not refer to a class member variale without a class pointer?
pd3dApp->m_dwAdapter = dwNewAdapter;

This code is likely outside the class methods for one, and the pointer in this example is static, which requires full qualification.

I''d suggest you learn C++ before - not simultaneously - attempting to tackle DirectX and other non-language topics.

Share this post


Link to post
Share on other sites
Oluseyi you have been graet
quote:

None of your questions have anything to do with DirectX.

I realize that it is not a DX question, this is the only reason I poste it in general programming.

I do not know about others but I found the best way to learn C++ is to learn DX simelteniously. I know this sounds odd but it is working for me regardless of some dum questions which I am not ashamed about, and regardless of distructive comments from Anonymous Poster.

The buttom line if I were an exprienced programmer and someone post what I think dumm question, then I just do not reply. Because no matter wwho is that person, I love to see him learn and posting such comments does not help the world evolve.

Thanks

Share this post


Link to post
Share on other sites
quote:
Original post by HappyToLearnC
Oluseyi you have been graet

Thank you. Post if you have any further questions.

quote:

I realize that it is not a DX question, this is the only reason I poste it in general programming.

I noticed, and I was impressed in a way.

quote:
I do not know about others but I found the best way to learn C++ is to learn DX simelteniously.

You know what? Whatever works for you. Good luck.

Share this post


Link to post
Share on other sites