473,385 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

que on Pointer to a class instance which is stored in a global variable

33
here's the overview what ive done so far:
Expand|Select|Wrap|Line Numbers
  1.  
  2. class A{};
  3. class B{};public A
  4. class C{}:public A
  5.  
  6. //i need to create a function that keep track 
  7. //of num of active B and C instance
  8. //in other file.cpp and file.h 
  9.  
  10. std::vector<A*> getsize();
  11.  
  12.  
  13.  
im not sure whether im on the right track by
solving this using pointers to a class instance which store in global function?
in this case what does type A* means?

any help would be great.

cheers
Oct 15 '09 #1
1 1562
Banfa
9,065 Expert Mod 8TB
A * is exactly what it looks like. A pointer to type A. However if A is polymorphic through the use of virtual functions then you can use an A * to point to an instance of B or C and when you call the virtual functions via the A * base pointer it will actually call the implementations of the functions in B or C.

If your functions in A are not polymorphic (declared virtual) then when you call them on an instance of B or C through a base class pointer A * it will call the function defined in A.

This may make more sence with an example

Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3. public:
  4.     A();
  5.     virtual ~A(); /* Very important on a polymorphic/abstract 
  6.                       class to declare destructor virtual */
  7.  
  8.     void nonVirtualFun();
  9.     virtual void virtualFun();
  10. }
  11.  
  12. class B : A
  13. {
  14. public:
  15.     B();
  16.     virtual ~B(); /* Very important on a polymorphic/abstract 
  17.                       class to declare destructor virtual */
  18.  
  19.     void nonVirtualFun();
  20.     virtual void virtualFun();
  21. }
  22.  
  23. B *myB = new B;    // Create a B
  24. A *basePtr = myB;    // Take a base class pointer to it
  25.  
  26. myB->nonVirtualFun(); // Calls B::nonVirtualFun
  27. A->nonVirtualFun(); // Calls A::nonVirtualFun
  28.  
  29. myB->virtualFun(); // Calls B::virtualFun
  30. A->virtualFun(); // Calls B::virtualFun
  31.  
  32. delete myB;
  33.  
Oct 15 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Rob | last post by:
Please recommend best practice for adding a global variable to an ASP.net class that will take on a Request.Params("") value from the URL calling the aspx page to be used throughout in stored...
24
by: LP | last post by:
After a code review one coworker insisted that global are very dangerous. He didn't really give any solid reasons other than, "performance penalties", "hard to maintain", and "dangerous". I think...
9
by: Newbie | last post by:
Hello, I need to declare a global variable e.g. database connection handle. So that I hava an access to this variable all over my class. How can I do it in c#? Do I have to declare it as static...
4
by: Mark | last post by:
I'd like to take an instance of a class and store it in a database. I've marked my class as and am using the binary formatting code similar to...
4
by: C_Programmer | last post by:
Question#1: ========== ========== What is the default value for any Static Global variable? Example: A.c: =====
1
by: sharath B N | last post by:
hi, i am sort of newbie to python. I am trying to do a super Market simulation with OOP in python. I have problems with using a class instance as global... def generate (... ,....,...) " in...
9
by: Spiros Bousbouras | last post by:
Let's say I have a global variable int var which I want to be known to translation units T1 and T2, I want T1 to be able to read its value and modify it and T2 to be able to read its value but not...
53
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global...
12
by: titan nyquist | last post by:
I have a class with data and methods that use it. Everything is contained perfectly THE PROBLEM: A separate thread has to call a method in the current instantiation of this class. There is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.