Is it dangerous to use the following in vector? [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a member variable vector<Foo> m_list
In some methods, I create the auto variable a in Foo type
Foo a;
m_list.push_back(a)
Is it possible that m_list[0] becomes invalid?
Class Foo
{
vector<int> _colors;
vector<int> _flowers;
}

You seem to be worried about what will happen to m_list[0] once the local variable a goes out of scope. Don't be. push_back actually pushes a copy of a into m_list. Now; that may cause other problems if you were expecting a and m_list[0] to be the same thing, but as long as ClassA has a functioning copy constructor (and based on your edit, it does), trying to access a destroyed object won't be one of them.

Related

Why am I getting an exception with a push involved with a shared pointer? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
How do I pass a smart pointer into this class so that it may be pushed into.
This line:
auto alltransactions(atrans);
Declares a new variable named alltransactions, which has the same name as your member.
Your constructor should use member init list to ensure your member are initialized correctly:
struct my_class {
my_class(std::shared_ptr<std::vector<cl_order>> atrans) : transactions(atrans) {}
std::shared_ptr<std::vector<cl_order>> transactions;
};

Accessing struct member through unique_ptr gives segmentation fault [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
This is how I set pointer to struct. At runtime segmentation fault is thrown at second line. LoadedPDFInfo is struct in Canvas namespace
struct std::unique_ptr<Canvas::LoadedPDFInfo> pdfInfo;
pdfInfo->handle = ++currentPDFHandle;
pdfInfo->totalPageNum = FPDF_GetPageCount(doc);
First, std::unique_ptr is a class not a struct, so get rid of the struct prefix on the pdfInfo variable declaration. You were probably thinking of this instead:
std::unique_ptr<struct Canvas::LoadedPDFInfo> pdfInfo;
But even when declaring variables (or type-casting) using actual struct types, you still do not need the struct prefix. C needs that, C++ does not.
Second, your segfault is happening because you have merely declared the pdfInfo variable, but it is not actually pointing at a valid LoadedPDFInfo object, so using the -> operator is not a valid operation. Just like a regular pointer, std::unique_ptr (and std::auto_ptr, and std::shared_ptr) have to point at something in order to access that something's members. For example:
std::unique_ptr<Canvas::LoadedPDFInfo> pdfInfo(new Canvas::LoadedPDFInfo);

Base class variables were `not declared in its scope` [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I am trying to go through C++ inheritance and I have a problem with my code. I have the base class Caine and CaineCuPete derived from it. I get an error when I declare the derived class constructor.
CaineCuPete(int nPete) : Caine(cNume,cHeight,cWeight,cAge,cColor);
Error:
cNume was not declared in its scope. cHeight was not declared in its
scope. ...
You need to take these as input parameters in your derived class constructor.
CaineCuPete(int nPete, string cNume, double cHeight,
double cWeight, double cAge, int cColor ) : Caine(cNume,cHeight,cWeight,cAge,cColor),
Pete ( nPete )
{
}
This link explains:
http://www.learncpp.com/cpp-tutorial/114-constructors-and-initialization-of-derived-classes/
You should define your variables cNume,cHeight,cWeight,cAge,cColor before the first use. A common problem is that they are defined after the point of use, not before.

return this-> command in C++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
return this->
what is that mean in C++
..
using namespace std;
IOM ConfigurationManager::getIOM(int iomId) {
return this->IOMs[iomId];
..
the relevant part of the whole code is above.
The code that I wrote is from a huge project which was waiting for someone to finish. I am not good at C++ but I need to learn more not to lose that job. Anyway, the project is full of "return this->...." which I thought unnecessary, that's why I asked is there smt special that we should use that notation
This piece of code simply means that the IOM at index iomId in the IOMs array in the ConfigurationManager object is returned. Note that the this->IOMs is the same as IOMs in this case, thus it seems the this is only there for clarity.
this is a pointer to the current object. The -> operator allow you to access a member inside a pointer to an object.
Thus return this->IOMs[iomID] returns the IOM object in the current ConfigurationManager at index iomID.

Why does the compilation fail for this code sample (in-class initial )in c++? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
class Test {
public:
int xyz=10;
};
Why does the compilation fail in c++?
It's a new feature in C++11. compile your code with -std=c++11
You should initialize members with a constructor. See this thread: in-class initialization of non-static and non-const members for good information.
It is not a static member so initialize it in constructor.Non static member cannot be initialize without constructorAlso see this for further details about the initialization of static and non staic data