2

I'm trying to create a class that will have array of objects of another class as its member. This "lower" class constructor demands a parameter (no default c-tor), and I'm not sure how to do this.

In .hpp
class ProcessingElement : public sc_core::sc_module
{
public:
    ProcessingElement( sc_core::sc_module_name name );
    sc_core::sc_module_name name;
};

In .cpp
ProcessingElement::ProcessingElement( sc_core::sc_module_name name ) : name(name) {
    //not relevant
}

And "upper" class:

In .hpp
class QuadPE : public sc_core::sc_module
{
public:
    QuadPE( sc_core::sc_module_name name );
    ProcessingElement pe[4];
};

In .cpp
QuadPE::QuadPE( sc_core::sc_module_name name ) : pe[0]("PE0"), pe[1]("PE1"), pe[2]("PE2"), pe[3]("PE3") {
    //non relevant
}

This obviously produces error, but I'm not sure how to fix it. I would like to avoid using vectors, if possible, so some solutions I found on SO that include vectors are not perfect for me.

As a note, sc_core::sc_module_name is a typedef of const char* or something similar, sadly can't look it up right now.

Thank you.

0

1 Answer 1

3

Just aggregate initialize the array:

QuadPE::QuadPE( sc_core::sc_module_name name ) : pe{"PE0", "PE1", "PE2", "PE3"} {}

While you may not want to use std::vector, I still suggest you give std::array a look. It's an aggregate too, that serves as a thin (zero overhead) wrapper over a c-style array. Nevertheless, it has full value semantics and is a full-featured standard library container. So you may find it less clunky to work with.

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

1 Comment

Very obvious and yet still I couldn't think of it. Thank you.

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.