Hello

I want to pass a bidimensional vector to the constructor of all instances of MyClass . All instances must use and share and modify the same vector. And I want to keep a reference to that main_vector to use it later in the program

I am passing it by reference to MyClass. The problem is that when I assign it to ref_to_main_vector to use it later, I'm not saving the reference but creating a duplicate of it, so each instances has its own copy.

How can I make them all share the same variable? I'm not interested in passing it by a specific method, but this is the only way that I have been able to make it partially work

Thanks

marc

std::vector < std::vector< int > > ref_to_main_vector;

MyClass::MyClass( std::vector < std::vector< int > > & m_v){
	ref_to_main_vector = m_v;
}
void MyClass::update(){
	ref_to_main_vector.resize(3);
	ref_to_main_vector[2].resize(3);
	...	
}

///////////

int main( ){
    std::vector < std::vector< int > > main_vector;

    MyClass temp = new MyClass(main_vector);
	
    temp.update();

    return 0;
}

A couple quick questions first:

How many instances of "MyClass" do you intend to have in your final program?

Other than the declaration, do you intend to use this vector anywhere else outside "MyClass"?

This sounds like a candidate for a static member variable, but it depends on your answers.

Hi Fbody,

I have several (10 to 50) instances of "MyClass" and I'm using it in other classes too, this is why I didn't make it a static member variable

Thanks

marc

Hi Fbody,

I have several (10 to 50) instances of "MyClass" and I'm using it in other classes too, this is why I didn't make it a static member variable

Thanks

marc

How are "MyClass" and the "other classes" related? If at all?

What are they supposed to do?

Can the classes be organized into some sort of inheritance structure?

Right now, there's only other class, which controls the status of the program and cleans the bidimensional vector at some points but I could try to get rid off it, so it would be only "MyClass" using it

Is there any reason you can't incorporate the status monitoring and cleaning operations related to the vector as static methods within MyClass? You could then have otherClass send messages to MyClass to either request status or initiate cleaning operations.

Uh oh..
I incorporated everything into MyClass and now I remember why I was reluctant to make the vector a static member of the class: I always get this "non_lazy_ptr" error and I have never been able to solve it :\

__ZN14MyClass14main_vectorE$non_lazy_ptr in MyClass.o

This is now my code:

// Definition of MyClass
static std::vector < std::vector< int > > main_vector; // Declaration of main_vector inside of MyClass (static!)

MyClass::MyClass(){
	main_vector.resize(2)
	...
}
void MyClass::update(){
	ref_to_main_vector[2].resize(3);
	...
}

///////////

int main( ){

    MyClass temp = new MyClass();
    temp.update();

    return 0;
}

Did I declare it properly?

Thanks

Marc

Unfortunately, this little bit of code really doesn't tell me anything. It's not actually a class definition, it's a partial implementation. I'm afraid you're going to have to post more code than that. Please post the actual class definition, not an excerpt.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.