First of all, as you will see, I am a noobie with C++. I am sure you can teach me some things.
I am having a problem with a small example code I am using for getting used to pointers and vectors of pointers, and I have a doubt regarding the convenience of using a vector of pointers for my case.
1.) Convenience of its use: I am writing a small C++ software. Within this software the user can create through a GUI certain objects (called modules), with an undefined amount of different parameters inside (they could be a lot). For the shake of performance/saving memory, I thought it would be good for its management having a vector of pointers to them, instead of allocating the objects themselves in a vector. The user would also be capable of deleting these objects(modules). Is it sensible to use a vector of pointers in this context? I also thought of using references to them, but I saw I should rather use a reference wrapper.
2.) Particular example: In the next paragraphs find an example of a simple code I am using for practicing with the use of pointers and vector of pointers.
file NumberClass.h
#ifndef NUNMBERCLASS_H
#define NUNMBERCLASS_H
class Number{
private:
int n;
public:
Number();
Number(int n);
int square();
int getNumber();
};
#endif
file NumberClass.cpp
#include "NumberClass.h"
Number::Number(){n=0;}
Number::Number(int m){n=m;}
int Number::square(){return (n*n);}
int Number::getNumber(){return n;}
file NumbersContainerClass.h
#ifndef NUMBERSCONTAINERCLASS_H
#define NUMBERSCONTAINERCLASS_H
#include <vector>
#include "NumberClass.h"
class NumbersContainer{
private:
std::vector<Number*> numCont;
public:
void numberPush_back(Number n);
Number* getNumberByPosition(int j);
int sizeMethod();
int capacityMethod();
};
#endif
file NumbersContainerClass.cpp
#include "NumbersContainerClass.h"
void NumbersContainer::numberPush_back(Number n){numCont.push_back(&n);}
Number* NumbersContainer::getNumberByPosition(int j){
return numCont[j];
}
int NumbersContainer::sizeMethod(){return numCont.size();}
int NumbersContainer::capacityMethod(){return numCont.capacity();}
file main.cpp
#include "NumbersContainerClass.h"
#include "NumberClass.h"
#include <vector>
#include <iostream>
int main(){
Number n1;
Number n2(2);
Number n3(3);
Number n4(4);
Number n5(5);
std::cout << n1.getNumber() << std::endl;
std::cout << "n1 Address: " << &n1 << std::endl;
std::cout << n2.getNumber() << std::endl;
std::cout << "n2 Address: " << &n2 << std::endl;
std::cout << n3.getNumber() << std::endl;
std::cout << "n3 Address: " << &n3 << std::endl;
std::cout << n4.getNumber() << std::endl;
std::cout << "n4 Address: " << &n4 << std::endl;
std::cout << n5.getNumber() << std::endl;
std::cout << "n5 Address: " << &n5 << std::endl;
NumbersContainer container;
std::cout << "n1 Address: " << &n1 << std::endl;
container.numberPush_back(n1);
int k=0; //Here I just wanted to check if something changed by pasing the parameter like this.
std::cout << (*(container.getNumberByPosition(k))).getNumber() << std::endl;
std::cout << container.getNumberByPosition(k)->getNumber() << std::endl;
std::cout << "n2 Address: " << &n2 << std::endl;
container.numberPush_back(n2);
std::cout << (*(container.getNumberByPosition(1))).getNumber() << std::endl;
std::cout << container.getNumberByPosition(1)->getNumber() << std::endl;
std::cout << "n3 Address: " << &n3 << std::endl;
container.numberPush_back(n3);
std::cout << (*(container.getNumberByPosition(2))).getNumber() << std::endl;
std::cout << container.getNumberByPosition(2)->getNumber() << std::endl;
std::cout << "n4 Address: " << &n4 << std::endl;
container.numberPush_back(n4);
std::cout << (*(container.getNumberByPosition(3))).getNumber() << std::endl;
std::cout << container.getNumberByPosition(3)->getNumber() << std::endl;
std::cout << "n5 Address: " << &n5 << std::endl;
container.numberPush_back(n5);
std::cout << (*(container.getNumberByPosition(4))).getNumber() << std::endl;
std::cout << container.getNumberByPosition(4)->getNumber() << std::endl;
std::cout << container.getNumberByPosition(4) << std::endl;
std::cout << container.getNumberByPosition(3) << std::endl;
std::cout << container.getNumberByPosition(2) << std::endl;
std::cout << container.getNumberByPosition(1) << std::endl;
std::cout << container.getNumberByPosition(0) << std::endl;
std::cout << container.sizeMethod() << std::endl;
std::cout << container.capacityMethod() << std::endl;
return 0;
}
and the output to this is:
0
n1 Address: 0x7ffd73484028
2
n2 Address: 0x7ffd7348402c
3
n3 Address: 0x7ffd73484030
4
n4 Address: 0x7ffd73484034
5
n5 Address: 0x7ffd73484038
n1 Address: 0x7ffd73484028
32765
32765
n2 Address: 0x7ffd7348402c
32765
32765
n3 Address: 0x7ffd73484030
32765
32765
n4 Address: 0x7ffd73484034
32765
32765
n5 Address: 0x7ffd73484038
32765
32765
0x7ffd73483ff4
0x7ffd73483ff4
0x7ffd73483ff4
0x7ffd73483ff4
0x7ffd73483ff4
5
8
The code works fine when I return directly a Number type object, so "Number NumbersContainer::getNumberByPosition(int k)". Nevertheless, when I try to return a pointer to a Number (Number* NumbersContainer::getNumberByPosition(int k)", although apparently I get a reference to the desired Number type object (since I can use the operator -> or dereferenciate the output pointer and use the function defined for Number "int Number::getNumber()"), the result I get is nonsense to me. What am I missing or I did not understand? I have played around for a long while with the code, but I always end up at this point, so I assume I did not understand something.
Thank you in advance! Any correction/remark/observartions next to the exact questions are more than welcome.
Aucun commentaire:
Enregistrer un commentaire