0

I'm new to c++, coming from python.

I'm working on a ray tracer program, that can loop over a list of objects in the scene.

this "pseudo-code", which i know is not working, will illustrate what i want to do:

class A{public:
    int i;
    //methods 
};
class B{public:
    int i;
    //methods 
};


void f(MyType objs[]){
    for( MyType ob : objs){
         //do somthing to ob's members
        }

}


int main(){

    A a1,a2,a3;
    B b1,b2;

    //i don't now how but i want to create 
    //an array that contains a different 
    //instances of different 
    //typs A, B and C... 
    

    MyType objects[]=[a1,a2,a3,b1,b2];
    

    //and lastly i want to pass the array to my function 
    

    f(objects);
    
    
return 0;}

i tried to create a base class and make A and B inherent classes. how can i do that or is that simply impossible? , and if so how i can achieve similar result.

2 Answers 2

2

It's not quite impossible. It's just quite complicated because it doesn't fit well with the design of statically typed languages.

There are many different approaches that can be taken with variyig complexity depending on additional what restrictions you are able to adhere to. For example, you only have two types in the example. A possible solution using std::variant:

std::variant<A, B> objs[] {a1,a2,a3,b1,b2};
for(std::variant<A, B>& ob : objs){
     std::visit([](auto&& arg){
         arg.i = 42;
     }, ob);
}

Another, quite different, approach is object oriented hierarchies where you have an array of pointers to base class subobject of different derived class instances with virtual functions that implement polymorphic behaviour.

Sign up to request clarification or add additional context in comments.

Comments

0

There are several approaches:

  • As you array seems fixed, std::tuple might be used:

    std::tuple objs{a1,a2,a3,b1,b2};
    
    auto func = [](auto& a_or_b){ /*..*/ };
    std::apply([&](auto&... args){ (func(a_or_b), ...); }, objs);
    
  • As possible types seems fixed, std::variant might be used:

    std::vector<std::variant<A, B>> objs{a1,a2,a3,b1,b2};
    auto func = [](auto& a_or_b){ /*..*/ };
    
    for (auto& a_or_b : objs) {
        std::visit(func, a_or_b);
    }
    
  • Else the polymorphic way with abstract base:

    struct Obj {
        virtual ~Obj() = default;
        virtual void foo() = 0; // Better to have action/behavior instead of getter/setter
    };
    
    struct A : Obj {
        void foo() override;
        int i;
        // ...
    };
    struct B : Obj {
        void foo() override;
        int i;
        // ...
    };
    

    and

    std::vector<Obj*> objs{&a1,&a2,&a3,&b1,&b2};
    
    for (Obj* obj : objs) {
        obj->foo(); // A::i, B::i not visible, only Obj interface usable.
    }
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.