0

I have the following:

class Application{  
protected:
   //Many getters , setters, and other methods
   Application(char *, string, string, ApplicationConstructor &, float); //Application's Constructor     
   ApplicationConstructor &AppConstructor;
   UserOpinion *UserView;
   vector<ApplicationConstructor> &AppConstructorVector; // Vector with Application Constructor Objects
   vector<UserOpinion> *UserOpinionVector; // Vector pointer to User Opinion Objects
};

and this constrcutor:

//Constructor
Application::Application(char *applicationCode, string applicationName, 
                     string applicationVersion, ApplicationConstructor &appConstructor ,
                     float price ):
                     AppConstructor(appConstructor),
                     AppConstructorVector(*(new vector<ApplicationConstructor>())),
                     UserOpinionVector(new vector<UserOpinion>()){
    int i = strlen(applicationCode);
    ApplicationCode = new char[i+1];
    strncpy(ApplicationCode, applicationCode, (i+1));
    ApplicationName = applicationName;
    ApplicationVersion = applicationVersion;
    Price = price;
    UserView = nullptr;
 // UserOpinionVector = nullptr;
 // if (!(this->FindAppConstructorOnVector(this->AppConstructor))){
 //     this->AddToAppConstructorVector(this->AppConstructor);
 // }
}

if I delete the comments on UserOpinionVector = nullptr; it runs smoothly, but with the declaration up on constructor I get runtime error. How can I instatiate a new UserOpinionVector when Application Object instatiates, something like the obove code? The AppConstructorVector(*(new vector())) instatiates right, and PLEASE do not say me to change the way UserOpinionVector is declared as property, I have this requirement to accomplish. Thanks in advance!!!

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
  • 2
    Pointers to containers are rarely if ever needed. And pointers with `new[]` shouldn't be needed when you have `std::string`. – Some programmer dude May 23 '20 at 17:14
  • 1
    Also please explain `AppConstructorVector(*(new vector()))` - note `AppConstructorVector` is a reference. – Richard Critten May 23 '20 at 17:16
  • 1
    ```*(new vector())``` looks like a great way to get a memory leak – Jake Schmidt May 23 '20 at 17:16
  • Worse, they make your job harder. If `Application` [owns](https://stackoverflow.com/questions/49024982/what-is-ownership-of-resources-or-pointers) those `vector`s, and it looks like it does, it has to make sure they are [correctly copied, moved, and destroyed](https://en.cppreference.com/w/cpp/language/rule_of_three). This becomes exactly the sort of pain-in-the-neck that `vector` and other containers were designed to eliminate. – user4581301 May 23 '20 at 17:17
  • *(new vector()) is a reference to AppConstructorVector as the property is declared in the class, because the statement new vector() is a pointer and the *(pointer) is the actually object, but as Jake says is a memory leak and I do not understand why? Why do I have memory leak? – Konstantinos May 23 '20 at 17:22
  • It's not a memory leak yet. You can still use the reference to `delete` the vector when you are done with it. The problem is the reference can make it harder to remember that you have to `delete` the `vector`. – user4581301 May 23 '20 at 17:26

0 Answers0