#referencing object created in constructor c++?

60 messages · Page 1 of 1 (latest)

sweet sparrow
#

I have the following constructor and need to push the constructor object into a vector. How do I do so? My current method doesn't seem to work.
`
class A : public B(...)
{
vector<A> numObj;
A(int a){
numObj.push_back(A(3));
}

}

elder portal
#

you do know that variable is an instance variable, right?

#

which means that each instance of class A will have its own numObj variable

sweet sparrow
elder portal
#

show code

sweet sparrow
#

A.h:
`
#pragma once

class A: public B{
A(int a);
}
extern vector<A> numObj
A.cpp:
vector<A> numObj;
A::A(int b): B(b){
numObj.push(back(A(b));
}
`

elder portal
#

it would probably be better to just do numObj.push_back(*this);

#

does your program ever halt?

sweet sparrow
#

when before I added this class, the print statements worked just fine

elder portal
#

by halting i meant terminate

#

show code for main

elder portal
sweet sparrow
#

#include "<A.h>" int main(){ cout << "test"'; A test(3); cout << "test 2"; }

#

"test" is printed but not "test 2"

elder portal
sweet sparrow
elder portal
#

you can do code block with triple backticks btw

sweet sparrow
#

Oh

elder portal
sweet sparrow
#

Ill show my B.cpp code maybe thats the issue

elder portal
#

it absolutely isn't

sweet sparrow
#

B.cpp:

#include "A.h"
vector<A> numObj;

B(int b){
  numObj.push_back(A(b));
}
elder portal
#

well, unless the constructor for B exits your program

#

same thing

#

the infinite loop is still there

#

A() calls B() which calls A() which calls B()......

sweet sparrow
#

my code seems to work when I comment out numObj.push_back(A(b)); "test 2" is printed out

elder portal
#

what platform are you on?

sweet sparrow
#

Windows

elder portal
#

it's impossible that your program is terminating

#

is there an error message?

#

like out of memory?

#

idk

sweet sparrow
#

no nothing

elder portal
#

maybe the OS is smart enough to kill the program beforehand

#

what about exit code?

sweet sparrow
#

no

elder portal
#

idk how they work on windows

sweet sparrow
#

is there a better way to insert the constructor object into the vector then?

elder portal
#

on linux you cant just echo $? to get the error code of the previous command

sweet sparrow
elder portal
#

and i remove that line from B's constructor as well

sweet sparrow
elder portal
#

yeah, that's the best approach

sweet sparrow
#

but if i wanted to push back the A object from B's constructor is there a good way

#

to do so?

#

should I just do A::A(int a):B(*this){...}?

elder portal
#

maybe

#

although idk why you'd want to do this

#

and if B is supposed to be a super class it shouldn't be aware of A's existence

#

this is a general rule, ofc

sweet sparrow
#

The B class's vector is supposed to contain all the subclasses objects

#

so I call it everytime I create a new object for any class

elder portal
#

i can't help any further for now

#

but i'd advice you to rethink what exactly you're trying to achieve

#

because this seems somewhat counterintuitive (and even dangerous)

sweet sparrow
#

thank you for all the help, im very thankful