Tuesday, July 14, 2009

Diffrence between generic pointer and pointer in c language?

1. Generic pointer is nothing but void*.


2. A generic pointer/void* can be easily type casted to the desired datatype.


3. Main differenc between the generic pointer and pointer of specific datatype: The void* is capable of pointing to the variable of any datatype whereas pointer of specific datatype say int* can point only to the variable of type int.


4. Generic pointer is also used for taking the address of the variable alone.


4. Whenever you are using void*, typecasting is required to perform the operation on the content that void* points to.


5. Be careful when you are doing any arithmetic operation with void*.


What is meant by Smart Pointer in C++ Language?

Where it necessarily in use and advantages to use this pointer?


If possible give me small snippet

What is meant by Smart Pointer in C++ Language?
In C++ you can create objects that act like pointers, but are "smarter" (well depends on how they are implemented..). Theoretically speaking they are called smart pointers simply because it usually does more actions then a default pointer.


Of course when creating these kind of objects you must design them as to have the same interface as normal pointers, meaning support pointer operations like dereferencing, indirection..


In some books you won't find the term of smart pointers, but you'll find the term of design patterns and even Pattern Oriented Arhitecture. A pattern is an object that looks and feels like something else.


In C++ standard library, there are already included some "smart" pointers, and one of them is auto ptr for example. auto ptr is generally used when you don't want to worry about cleaning up the memory after using normal pointer (auto ptr cleans up after itself automatically).


You can even create a pointer that reacts differently when being deleted. (so called pointer to NULL...)


Can address(&) be used as a pointer in c ?

We have always assigned(stored) the address of a datatype in a special variable called pointer, and then used the pointer which points to the datatype to carry out various operations. Can anybody tell me if there is a way which allows instead of storing the address in a pointer variable, whether the address of the datatype can be directly used(like a pointer) to carry out the operations on the dataype. For example, let's take the small example :





# include %26lt;iostream.h%26gt;


# include %26lt;conio.h%26gt;





struct A


{


virtual void f()


{cout %26lt;%26lt; "Class A" %26lt;%26lt; endl;}


};





struct B:A


{


void f()


{cout %26lt;%26lt; "Class B" %26lt;%26lt; endl;}


};





int main()


{


A a;


B b;


A *ptr;


clrscr();


ptr = %26amp;a;


// My question is whether f() could be accessed somehow by using %26amp;a instead


// of using the ptr as done below


ptr -%26gt; f();


cout %26lt;%26lt; endl;


ptr = %26amp;b;


// same question for the below operation


ptr -%26gt; f();


getch();


}





I have included my question inside the code, please help

Can address(%26amp;) be used as a pointer in c ?
Yes and sort of...





By wrapping in parens, you force the expression (%26amp;a) to be resolved first, the type of the expression should be imputed as (A*), so after the first assignment of ptr, these:





(%26amp;a)-%26gt;f();


ptr-%26gt;f();





should be equivilent. However, b is (obviously) of type B, so the type of (%26amp;b) should be imputed as (B*), whereas ptr is type (A*). As such, to get the equivilent of the second assignment of ptr (without assigning a pointer) you need a type cast to override the imputed type, e.g.,





(A*)(%26amp;b)-%26gt;f();








Make sense?
Reply:Sure.





A a;


B b;


A *aa = new A();


B *bb = new B();





a.f(); /*prints Class A*/


b.f(); /* prints Calls B */


aa-%26gt;f(); /*prints Class A*/


bb-%26gt;f(); /* prints Calls B */





// playing with v-tables


b.A::f(); /* prints Class A, uses the B object data with the logic from A's f() */


bb-%26gt;A::f() /* prints Class A, uses the B object data with the logic from A's f() */
Reply:Absolutely yes. You can.


C language help regarding Mouse pointer?

well.. how to make the mouse pointer appear on a project in c??


i have the coding writen in a book which makes the mouse pointer appear in the program also reads if the mouse bottom is clicked or not but i just can seem to understand the terms used in making the program i searched the net but cannot find a specific article related to implement or initializemouse in c with clear deiscription ?? any particular sites would be helpful or a detail explanation thanku?

C language help regarding Mouse pointer?
Anything gui related will be handled by X. Assuming you're using the Xorg server (and not Xfree), then take a look over here:





http://www.x.org/wiki/Development/Docume...
Reply:In the ancient MS-Dos times mouses work on Serial port so programming is easy But now you have to know how to controll PS/2 and USB.





Check this site : http://www.daniweb.com/forums/post415481...

crab apple

C language help regarding Mouse pointer?

i am not using any server or .. it plain winxp and plain turbo c editor have made a game using graphics and ofcourse keyboard i want to have mouse interface too ?help?








C language help regarding Mouse pointer?


well.. how to make the mouse pointer appear on a project in c??


i have the coding writen in a book which makes the mouse pointer appear in the program also reads if the mouse bottom is clicked or not but i just can seem to understand the terms used in making the program i searched the net but cannot find a specific article related to implement or initializemouse in c with clear deiscription ?? any particular sites would be helpful or a detail explanation thanku?

C language help regarding Mouse pointer?
Bestever resource is


http://www.programmersheavan.com








Cheers:)
Reply:C language? So let me get this, you created a game using the graphics api? Why not use opengl/direct3d or whatever. Is your game fullscreen?


Polymorphism in C++?

Does C++ require that pointers be used to use Polymorphism in C++?





Why I ask:


I created a simple class Vehicle which has nothing except a member function called "showType()" which displays "I am a vehicle". I then created a subclass of Vehicle, Car, with a function called "showType()" which displays "I am a car". The function showType() is marked as virtual in the base class.





If I write the following code:


Vehicle test[10] ;


Car c ;


test[0] = c ;


test[0].showType() ;


The output "I am a Vehicle is produced" -- clearly no polymorphism.





However, if I change to using pointers:


Vehicle* test[10] ;


Car c ;


test[0] = %26amp;c ;


test[0]-%26gt;showType() ;





This shows the result I expect -- "I am a car". Does C++ only support Polymorphism with pointers? In some sense this seems to be logical since Polymorphism is a runtime feature, however I was a little disappointed that it couldn't distinguish in the non-pointer case -- I was hoping for a container type effect similar to Java.

Polymorphism in C++?
C++ always uses pointers to work with polymorphism and other OOP subjects.


Polymorphism in C++?

Does C++ require that pointers be used to use Polymorphism in C++?





Why I ask:


I created a simple class Vehicle which has nothing except a member function called "showType()" which displays "I am a vehicle". I then created a subclass of Vehicle, Car, with a function called "showType()" which displays "I am a car". The function showType() is marked as virtual in the base class.





If I write the following code:


Vehicle test[10] ;


Car c ;


test[0] = c ;


test[0].showType() ;


The output "I am a Vehicle is produced" -- clearly no polymorphism.





However, if I change to using pointers:


Vehicle* test[10] ;


Car c ;


test[0] = %26amp;c ;


test[0]-%26gt;showType() ;





This shows the result I expect -- "I am a car". Does C++ only support Polymorphism with pointers? In some sense this seems to be logical since Polymorphism is a runtime feature, however I was a little disappointed that it couldn't distinguish in the non-pointer case -- I was hoping for a container type effect similar to Java.

Polymorphism in C++?
The difference, Sharpie, between


test[0] = c ;


and


test[0] = %26amp;c ;


is that in the first case you are copying an instance of something to a Vehicle. In that event, all of c that is a Vehicle will be copied to test[0], and test[0] will have none of the Carness. C will remain a Car and test[0] will remain only a Vehicle.


However, %26amp;c is a pointer, so regardless of type it is cast to, it will always be a pointer to an instance of a Car, even if it is cast to a Vehicle pointer.


As a rule of thumb, yes, you will need to deal only with pointers if you are going to make polymorphism work. However, according to the technical definition, a reference is not a pointer, but as far as the machine code is concerned, it is, so this


Vehicle %26amp;v = c;


v.showType();


would also reveal it to be a Car.
Reply:That is the correct behavior in both cases.





You will only see polymorphic behavior with pointers and references.





My java is pretty rusty but I don't recall that it is different in this respect. I suspect you were storing references (whether or not you realized it) in the containers.
Reply:Its supported!!





I also am studying programming and do not know but here:


http://www.google.com/search?hl=en%26amp;q=pol...