Creating an Array of Pointers to Objects in C++ Without Using Virtual Functions

In summary, the conversation revolves around creating a 4th class, Array_Class, that will have 2 pointers array for classes A_1 and A_2. The issue is how to get an object pointer from either class and place it in the correct array without using virtual functions. One solution proposed is to use overloading instead of casting or virtual functions.
  • #1
erezb84
43
0

Homework Statement


suppose i have class A.
now i have 2 derative classes: A_1, A_2.

i would like to create a 4'th class: Array_Class.
Array_Class will have 2 pointers array (one for each class - A_1, A_2).
i want to be able to get an object pointer from class A_1 or A_2, and place it in the right array.
how should i do it?
another importent issue: i am not allowed to use virtual functions.

The Attempt at a Solution


I thought of saving in each class (A_1, A_2) a type variable, and in Array_Class to do switch or somthing to know in which array save the pointer.
I know this is no good proggraming, but is there bettwer way to do it ? (again - without using virtual functions).

hope i was clear...
Thanks!
 
Physics news on Phys.org
  • #2
Wait, so what you want is something like this?
Code:
class { 
  A1** a1s;
  A2** a2s;
 
  void addPointer(A* a) { 
    // add a to a1s or a2s depending on its type
  }

That is, as you say, terrible programming and any teacher giving you such an assignment should be locked away!

You should make a single array of pointers to A's, and use polymorphism, i.e. so you can call
Code:
A* a = arrayOfAs[i];
a->method();
without knowing if a is of subtype A1 or A2.
As soon as your find yourself writing checks like "if(a is of type A1) { doSomething(); } else if(a is of type A2) { doSomethingElse(); }" you should seriously reconsider your design or else you may just as well switch back to C and get rid of those pesky object-thingies.

That said, if you keep insisting on abusing the features of C++, the "a is of type A1" line in my pseudocode above can be implemented with dynamic_cast:
Code:
bool isA1 = !(dynamic_cast<A1*>(a) == null);
 
  • #3
Yes, this is the excercise we got.
and we got the instruction - not use virtual function, not use casting...
so i think even the "terrible" "a is of type A1" i can't write...
 
  • #4
Use overloading.
Code:
class Array_Class {
   add_pointer (A_1 * ptr);
   add_pointer (A_2 * ptr);
   ...
};
 
  • #5
Thanks! i think overloading is the answer!
 

Related to Creating an Array of Pointers to Objects in C++ Without Using Virtual Functions

What is an object in C++?

An object in C++ is a specific instance of a class. It is a data structure that contains data members (attributes) and member functions (methods) that operate on that data. Objects are used to model real-world entities and their interactions.

What is a pointer in C++?

A pointer in C++ is a variable that stores the memory address of another variable. It is used to indirectly access or manipulate data stored in memory. Pointers are commonly used in C++ to pass parameters by reference, to dynamically allocate memory, and to create data structures like linked lists and trees.

How do you create an array of objects in C++?

To create an array of objects in C++, you first need to define a class and then declare an array of that class. For example, if you have a class called "Person", you can declare an array of 10 Person objects by writing "Person myArray[10];". This will create an array of 10 Person objects, each with its own set of data members and member functions.

Can you use pointers to access objects in an array in C++?

Yes, you can use pointers to access objects in an array in C++. Pointers can be used to point to individual objects in the array and to access their data members and member functions. For example, if you have a pointer called "ptr" and an array of Person objects called "myArray", you can use "ptr = &myArray[0]" to point to the first object in the array.

How do you delete an array of objects in C++?

To delete an array of objects in C++, you can use the "delete[]" operator followed by the name of the array. This will free the memory allocated for the array of objects. It is important to note that when using "delete[]" on an array of objects, the destructor for each object in the array will be called.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
5K
  • Programming and Computer Science
Replies
23
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
910
  • Programming and Computer Science
2
Replies
40
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
966
Back
Top