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

  • Context: Comp Sci 
  • Thread starter Thread starter erezb84
  • Start date Start date
  • Tags Tags
    Array C++ Pointers
Click For Summary

Discussion Overview

The discussion revolves around creating an array of pointers to objects in C++ without using virtual functions. Participants explore various approaches to manage pointers from derived classes A_1 and A_2 within a new class, Array_Class, while adhering to specific constraints set by an assignment.

Discussion Character

  • Homework-related
  • Debate/contested
  • Technical explanation

Main Points Raised

  • One participant describes the requirement to create a class Array_Class that holds two arrays of pointers, one for each derived class A_1 and A_2, and seeks advice on how to implement this without virtual functions.
  • Another participant suggests that the proposed method of using a type variable and a switch statement is poor programming practice and recommends using a single array of pointers to the base class A, leveraging polymorphism instead.
  • A later reply emphasizes that the assignment constraints prohibit the use of virtual functions and casting, which limits the design options available.
  • One participant proposes using function overloading in Array_Class to handle pointers to A_1 and A_2 separately, suggesting this as a potential solution to the problem.
  • Another participant expresses agreement that overloading may be a suitable approach to meet the assignment requirements.

Areas of Agreement / Disagreement

Participants express disagreement on the appropriateness of certain programming practices, with some advocating for a more polymorphic approach while others are constrained by the assignment's rules. There is no consensus on a definitive solution, as the discussion reflects varying opinions on how to best address the problem within the given constraints.

Contextual Notes

The discussion is limited by the specific requirement to avoid virtual functions and casting, which constrains the design options available to participants. The effectiveness of proposed solutions may depend on interpretations of the assignment's guidelines.

erezb84
Messages
43
Reaction score
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
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);
 
Yes, this is the exercise 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...
 
Use overloading.
Code:
class Array_Class {
   add_pointer (A_1 * ptr);
   add_pointer (A_2 * ptr);
   ...
};
 
Thanks! i think overloading is the answer!
 

Similar threads

  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 8 ·
Replies
8
Views
7K
Replies
7
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 23 ·
Replies
23
Views
2K