Correct allocation for std::vectorHow to pass objects to functions in C++?Concatenating two std::vectorsWhat's the best way to trim std::string?How to convert a std::string to const char* or char*?std::wstring VS std::stringHow to find out if an item is present in a std::vector?Why is “using namespace std” considered bad practice?What is the easiest way to initialize a std::vector with hardcoded elements?Appending a vector to a vectorC++ LNK Error 2019Vector push_back error when compiling
Why "Having chlorophyll without photosynthesis is actually very dangerous" and "like living with a bomb"?
Replacing matching entries in one column of a file by another column from a different file
Why doesn't H₄O²⁺ exist?
Alternative to sending password over mail?
How can I prevent hyper evolved versions of regular creatures from wiping out their cousins?
Why can't we play rap on piano?
Why doesn't Newton's third law mean a person bounces back to where they started when they hit the ground?
Is it inappropriate for a student to attend their mentor's dissertation defense?
Is it possible to do 50 km distance without any previous training?
Why can't I see bouncing of switch on oscilloscope screen?
A case of the sniffles
dbcc cleantable batch size explanation
What's that red-plus icon near a text?
How to format long polynomial?
What is the word for reserving something for yourself before others do?
Is it legal for company to use my work email to pretend I still work there?
Does detail obscure or enhance action?
Important Resources for Dark Age Civilizations?
Uncaught TypeError: 'set' on proxy: trap returned falsish for property Name
Theorems that impeded progress
Can a vampire attack twice with their claws using multiattack?
Is it possible to run Internet Explorer on OS X El Capitan?
What is a clear way to write a bar that has an extra beat?
Why does Kotter return in Welcome Back Kotter?
Correct allocation for std::vector
How to pass objects to functions in C++?Concatenating two std::vectorsWhat's the best way to trim std::string?How to convert a std::string to const char* or char*?std::wstring VS std::stringHow to find out if an item is present in a std::vector?Why is “using namespace std” considered bad practice?What is the easiest way to initialize a std::vector with hardcoded elements?Appending a vector to a vectorC++ LNK Error 2019Vector push_back error when compiling
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a problem giving my std::vector to another class. I put data into the std::vector and put it into a class called "Mesh". And the "Mesh" comes into a "Model".
// Store the vertices
std::vector<float> positionVertices;
positionVertices.push_back(-0.5f);
positionVertices.push_back(-0.5f);
positionVertices.push_back( 0.5f);
positionVertices.push_back(-0.5f);
positionVertices.push_back(-0.5f);
positionVertices.push_back( 0.5f);
// Put them into a mesh and the mesh into a model
Mesh mesh = Mesh(positionVertices);
Model model = Model(mesh);
In the model class, I take the position vertices of the mesh back and convert it into a float[]. But it seems like that, that the way I allocate the std::vector is wrong, because when checking the std::vector in the model class, it has a size of 0.
// Store the vertices
float* dataPtr = &data[0];
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), dataPtr, GL_STATIC_DRAW);
How can I bring the data correctly into the other classes?
I'm also unsure about the way the constructor for the mesh class works.
Mesh.h:
// Mesh.h
class Mesh
public:
std::vector<float> positionVertices;
Mesh(std::vector<float>);
~Mesh();
;
Mesh.cpp:
// Mesh.cpp
Mesh::Mesh(std::vector<float> positionVertices) : positionVertices(Mesh::positionVertices)
Model.h:
// Model.h
class Model
public:
Mesh mesh;
unsigned int vertexArray;
unsigned int vertexCount;
Model(Mesh);
~Model();
void storeData(std::vector<float> data, const unsigned int index, const unsigned int size);
;
Model.cpp:
// Model.cpp
Model::Model(Mesh mesh) : mesh(Model::mesh)
...
c++ opengl
add a comment |
I have a problem giving my std::vector to another class. I put data into the std::vector and put it into a class called "Mesh". And the "Mesh" comes into a "Model".
// Store the vertices
std::vector<float> positionVertices;
positionVertices.push_back(-0.5f);
positionVertices.push_back(-0.5f);
positionVertices.push_back( 0.5f);
positionVertices.push_back(-0.5f);
positionVertices.push_back(-0.5f);
positionVertices.push_back( 0.5f);
// Put them into a mesh and the mesh into a model
Mesh mesh = Mesh(positionVertices);
Model model = Model(mesh);
In the model class, I take the position vertices of the mesh back and convert it into a float[]. But it seems like that, that the way I allocate the std::vector is wrong, because when checking the std::vector in the model class, it has a size of 0.
// Store the vertices
float* dataPtr = &data[0];
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), dataPtr, GL_STATIC_DRAW);
How can I bring the data correctly into the other classes?
I'm also unsure about the way the constructor for the mesh class works.
Mesh.h:
// Mesh.h
class Mesh
public:
std::vector<float> positionVertices;
Mesh(std::vector<float>);
~Mesh();
;
Mesh.cpp:
// Mesh.cpp
Mesh::Mesh(std::vector<float> positionVertices) : positionVertices(Mesh::positionVertices)
Model.h:
// Model.h
class Model
public:
Mesh mesh;
unsigned int vertexArray;
unsigned int vertexCount;
Model(Mesh);
~Model();
void storeData(std::vector<float> data, const unsigned int index, const unsigned int size);
;
Model.cpp:
// Model.cpp
Model::Model(Mesh mesh) : mesh(Model::mesh)
...
c++ opengl
1
off-topic:std::vector<float> positionVertices-0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f;
– bolov
Mar 21 at 22:46
2
I don't see any error in the provided code, it's probably somewhere else. Minimal, Complete, and Verifiable example please.
– HolyBlackCat
Mar 21 at 22:46
There is no obvious problem with the shown code, therefore the problem must be in the code that's not shown.
– Sam Varshavchik
Mar 21 at 22:47
Can you show the constructors for theMeshandModelclasses? @maniel34
– Chase
Mar 21 at 22:53
@Chase Yeah, I put them now into the question. Now, I also think that must be the reason.
– maniel34
Mar 21 at 22:57
add a comment |
I have a problem giving my std::vector to another class. I put data into the std::vector and put it into a class called "Mesh". And the "Mesh" comes into a "Model".
// Store the vertices
std::vector<float> positionVertices;
positionVertices.push_back(-0.5f);
positionVertices.push_back(-0.5f);
positionVertices.push_back( 0.5f);
positionVertices.push_back(-0.5f);
positionVertices.push_back(-0.5f);
positionVertices.push_back( 0.5f);
// Put them into a mesh and the mesh into a model
Mesh mesh = Mesh(positionVertices);
Model model = Model(mesh);
In the model class, I take the position vertices of the mesh back and convert it into a float[]. But it seems like that, that the way I allocate the std::vector is wrong, because when checking the std::vector in the model class, it has a size of 0.
// Store the vertices
float* dataPtr = &data[0];
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), dataPtr, GL_STATIC_DRAW);
How can I bring the data correctly into the other classes?
I'm also unsure about the way the constructor for the mesh class works.
Mesh.h:
// Mesh.h
class Mesh
public:
std::vector<float> positionVertices;
Mesh(std::vector<float>);
~Mesh();
;
Mesh.cpp:
// Mesh.cpp
Mesh::Mesh(std::vector<float> positionVertices) : positionVertices(Mesh::positionVertices)
Model.h:
// Model.h
class Model
public:
Mesh mesh;
unsigned int vertexArray;
unsigned int vertexCount;
Model(Mesh);
~Model();
void storeData(std::vector<float> data, const unsigned int index, const unsigned int size);
;
Model.cpp:
// Model.cpp
Model::Model(Mesh mesh) : mesh(Model::mesh)
...
c++ opengl
I have a problem giving my std::vector to another class. I put data into the std::vector and put it into a class called "Mesh". And the "Mesh" comes into a "Model".
// Store the vertices
std::vector<float> positionVertices;
positionVertices.push_back(-0.5f);
positionVertices.push_back(-0.5f);
positionVertices.push_back( 0.5f);
positionVertices.push_back(-0.5f);
positionVertices.push_back(-0.5f);
positionVertices.push_back( 0.5f);
// Put them into a mesh and the mesh into a model
Mesh mesh = Mesh(positionVertices);
Model model = Model(mesh);
In the model class, I take the position vertices of the mesh back and convert it into a float[]. But it seems like that, that the way I allocate the std::vector is wrong, because when checking the std::vector in the model class, it has a size of 0.
// Store the vertices
float* dataPtr = &data[0];
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), dataPtr, GL_STATIC_DRAW);
How can I bring the data correctly into the other classes?
I'm also unsure about the way the constructor for the mesh class works.
Mesh.h:
// Mesh.h
class Mesh
public:
std::vector<float> positionVertices;
Mesh(std::vector<float>);
~Mesh();
;
Mesh.cpp:
// Mesh.cpp
Mesh::Mesh(std::vector<float> positionVertices) : positionVertices(Mesh::positionVertices)
Model.h:
// Model.h
class Model
public:
Mesh mesh;
unsigned int vertexArray;
unsigned int vertexCount;
Model(Mesh);
~Model();
void storeData(std::vector<float> data, const unsigned int index, const unsigned int size);
;
Model.cpp:
// Model.cpp
Model::Model(Mesh mesh) : mesh(Model::mesh)
...
c++ opengl
c++ opengl
edited Mar 21 at 22:56
maniel34
asked Mar 21 at 22:43
maniel34maniel34
396
396
1
off-topic:std::vector<float> positionVertices-0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f;
– bolov
Mar 21 at 22:46
2
I don't see any error in the provided code, it's probably somewhere else. Minimal, Complete, and Verifiable example please.
– HolyBlackCat
Mar 21 at 22:46
There is no obvious problem with the shown code, therefore the problem must be in the code that's not shown.
– Sam Varshavchik
Mar 21 at 22:47
Can you show the constructors for theMeshandModelclasses? @maniel34
– Chase
Mar 21 at 22:53
@Chase Yeah, I put them now into the question. Now, I also think that must be the reason.
– maniel34
Mar 21 at 22:57
add a comment |
1
off-topic:std::vector<float> positionVertices-0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f;
– bolov
Mar 21 at 22:46
2
I don't see any error in the provided code, it's probably somewhere else. Minimal, Complete, and Verifiable example please.
– HolyBlackCat
Mar 21 at 22:46
There is no obvious problem with the shown code, therefore the problem must be in the code that's not shown.
– Sam Varshavchik
Mar 21 at 22:47
Can you show the constructors for theMeshandModelclasses? @maniel34
– Chase
Mar 21 at 22:53
@Chase Yeah, I put them now into the question. Now, I also think that must be the reason.
– maniel34
Mar 21 at 22:57
1
1
off-topic:
std::vector<float> positionVertices-0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f;– bolov
Mar 21 at 22:46
off-topic:
std::vector<float> positionVertices-0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f;– bolov
Mar 21 at 22:46
2
2
I don't see any error in the provided code, it's probably somewhere else. Minimal, Complete, and Verifiable example please.
– HolyBlackCat
Mar 21 at 22:46
I don't see any error in the provided code, it's probably somewhere else. Minimal, Complete, and Verifiable example please.
– HolyBlackCat
Mar 21 at 22:46
There is no obvious problem with the shown code, therefore the problem must be in the code that's not shown.
– Sam Varshavchik
Mar 21 at 22:47
There is no obvious problem with the shown code, therefore the problem must be in the code that's not shown.
– Sam Varshavchik
Mar 21 at 22:47
Can you show the constructors for the
Mesh and Model classes? @maniel34– Chase
Mar 21 at 22:53
Can you show the constructors for the
Mesh and Model classes? @maniel34– Chase
Mar 21 at 22:53
@Chase Yeah, I put them now into the question. Now, I also think that must be the reason.
– maniel34
Mar 21 at 22:57
@Chase Yeah, I put them now into the question. Now, I also think that must be the reason.
– maniel34
Mar 21 at 22:57
add a comment |
1 Answer
1
active
oldest
votes
// Mesh.cpp
Mesh::Mesh(std::vector<float> positionVertices) :
positionVertices(Mesh::positionVertices) // Here's the problem
the positionVertices in the initializer list is Mesh::positionVertices, so you're assigning it to itself.
use
positionVertices(positionVertices)
Also, change
Mesh::Mesh(std::vector<float> positionVertices) :
to
Mesh::Mesh(const std::vector<float>& positionVertices) :
so you're not making needless copies of your vector.
I disagree with the last change: taking thevectorby reference won't help, as thevectorwill still get copied intoMesh::positionVertices. One could move it, but otherwise I'd keep it as a copy so the interface reads better (the interface of Mesh says "I copy this" rather than "I might store a reference to this, so ensure you keep it alive for the entire lifetime of me", which makes a difference)
– Tas
Mar 21 at 23:11
@Tas if it's not passed by const ref it will be copied from the original variable into the constructor call, then again into the member variable. I would direct your attention to stackoverflow.com/questions/2139224/…
– Tzalumen
Mar 21 at 23:13
I highly doubt any compiler worth its weight wouldn't omit one of those copies, and for me personally I'd rather my interface be clear a copy is taken, rather than users having to worry about a stored reference. Assuming OP isn't creating 10000Meshwith vectors that are 10000 big, even if there are copies it's very likely meaningless. Regardless, these are just my opinions and I didn't downvote and your answer is still correct.
– Tas
Mar 22 at 0:01
It is clear, because you cannot store aconst&(without pointer shenanigans that you should not be doing), you can only read and copy from it. This is why I believe your opinion to be misguided, this is the gist of the article I linked to, and why I linked that article.
– Tzalumen
Mar 25 at 17:13
Of course you can store aconst std::vector<float>&, why wouldn't you be able to? You have a reference to the originalvector, which when it changes you receive those changes, but you are unable to change it. So it becomes problematic if a local vector is made, then given to an instance ofMeshwhich outlives thevector. Of course in any given small example things will be fine, but I have professionally encountered issues with passing references. For any given function they're preferred, but into a constructor is a bad idea since the class may store a reference.
– Tas
Mar 25 at 21:58
|
show 4 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55290328%2fcorrect-allocation-for-stdvector%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
// Mesh.cpp
Mesh::Mesh(std::vector<float> positionVertices) :
positionVertices(Mesh::positionVertices) // Here's the problem
the positionVertices in the initializer list is Mesh::positionVertices, so you're assigning it to itself.
use
positionVertices(positionVertices)
Also, change
Mesh::Mesh(std::vector<float> positionVertices) :
to
Mesh::Mesh(const std::vector<float>& positionVertices) :
so you're not making needless copies of your vector.
I disagree with the last change: taking thevectorby reference won't help, as thevectorwill still get copied intoMesh::positionVertices. One could move it, but otherwise I'd keep it as a copy so the interface reads better (the interface of Mesh says "I copy this" rather than "I might store a reference to this, so ensure you keep it alive for the entire lifetime of me", which makes a difference)
– Tas
Mar 21 at 23:11
@Tas if it's not passed by const ref it will be copied from the original variable into the constructor call, then again into the member variable. I would direct your attention to stackoverflow.com/questions/2139224/…
– Tzalumen
Mar 21 at 23:13
I highly doubt any compiler worth its weight wouldn't omit one of those copies, and for me personally I'd rather my interface be clear a copy is taken, rather than users having to worry about a stored reference. Assuming OP isn't creating 10000Meshwith vectors that are 10000 big, even if there are copies it's very likely meaningless. Regardless, these are just my opinions and I didn't downvote and your answer is still correct.
– Tas
Mar 22 at 0:01
It is clear, because you cannot store aconst&(without pointer shenanigans that you should not be doing), you can only read and copy from it. This is why I believe your opinion to be misguided, this is the gist of the article I linked to, and why I linked that article.
– Tzalumen
Mar 25 at 17:13
Of course you can store aconst std::vector<float>&, why wouldn't you be able to? You have a reference to the originalvector, which when it changes you receive those changes, but you are unable to change it. So it becomes problematic if a local vector is made, then given to an instance ofMeshwhich outlives thevector. Of course in any given small example things will be fine, but I have professionally encountered issues with passing references. For any given function they're preferred, but into a constructor is a bad idea since the class may store a reference.
– Tas
Mar 25 at 21:58
|
show 4 more comments
// Mesh.cpp
Mesh::Mesh(std::vector<float> positionVertices) :
positionVertices(Mesh::positionVertices) // Here's the problem
the positionVertices in the initializer list is Mesh::positionVertices, so you're assigning it to itself.
use
positionVertices(positionVertices)
Also, change
Mesh::Mesh(std::vector<float> positionVertices) :
to
Mesh::Mesh(const std::vector<float>& positionVertices) :
so you're not making needless copies of your vector.
I disagree with the last change: taking thevectorby reference won't help, as thevectorwill still get copied intoMesh::positionVertices. One could move it, but otherwise I'd keep it as a copy so the interface reads better (the interface of Mesh says "I copy this" rather than "I might store a reference to this, so ensure you keep it alive for the entire lifetime of me", which makes a difference)
– Tas
Mar 21 at 23:11
@Tas if it's not passed by const ref it will be copied from the original variable into the constructor call, then again into the member variable. I would direct your attention to stackoverflow.com/questions/2139224/…
– Tzalumen
Mar 21 at 23:13
I highly doubt any compiler worth its weight wouldn't omit one of those copies, and for me personally I'd rather my interface be clear a copy is taken, rather than users having to worry about a stored reference. Assuming OP isn't creating 10000Meshwith vectors that are 10000 big, even if there are copies it's very likely meaningless. Regardless, these are just my opinions and I didn't downvote and your answer is still correct.
– Tas
Mar 22 at 0:01
It is clear, because you cannot store aconst&(without pointer shenanigans that you should not be doing), you can only read and copy from it. This is why I believe your opinion to be misguided, this is the gist of the article I linked to, and why I linked that article.
– Tzalumen
Mar 25 at 17:13
Of course you can store aconst std::vector<float>&, why wouldn't you be able to? You have a reference to the originalvector, which when it changes you receive those changes, but you are unable to change it. So it becomes problematic if a local vector is made, then given to an instance ofMeshwhich outlives thevector. Of course in any given small example things will be fine, but I have professionally encountered issues with passing references. For any given function they're preferred, but into a constructor is a bad idea since the class may store a reference.
– Tas
Mar 25 at 21:58
|
show 4 more comments
// Mesh.cpp
Mesh::Mesh(std::vector<float> positionVertices) :
positionVertices(Mesh::positionVertices) // Here's the problem
the positionVertices in the initializer list is Mesh::positionVertices, so you're assigning it to itself.
use
positionVertices(positionVertices)
Also, change
Mesh::Mesh(std::vector<float> positionVertices) :
to
Mesh::Mesh(const std::vector<float>& positionVertices) :
so you're not making needless copies of your vector.
// Mesh.cpp
Mesh::Mesh(std::vector<float> positionVertices) :
positionVertices(Mesh::positionVertices) // Here's the problem
the positionVertices in the initializer list is Mesh::positionVertices, so you're assigning it to itself.
use
positionVertices(positionVertices)
Also, change
Mesh::Mesh(std::vector<float> positionVertices) :
to
Mesh::Mesh(const std::vector<float>& positionVertices) :
so you're not making needless copies of your vector.
answered Mar 21 at 23:05
TzalumenTzalumen
486212
486212
I disagree with the last change: taking thevectorby reference won't help, as thevectorwill still get copied intoMesh::positionVertices. One could move it, but otherwise I'd keep it as a copy so the interface reads better (the interface of Mesh says "I copy this" rather than "I might store a reference to this, so ensure you keep it alive for the entire lifetime of me", which makes a difference)
– Tas
Mar 21 at 23:11
@Tas if it's not passed by const ref it will be copied from the original variable into the constructor call, then again into the member variable. I would direct your attention to stackoverflow.com/questions/2139224/…
– Tzalumen
Mar 21 at 23:13
I highly doubt any compiler worth its weight wouldn't omit one of those copies, and for me personally I'd rather my interface be clear a copy is taken, rather than users having to worry about a stored reference. Assuming OP isn't creating 10000Meshwith vectors that are 10000 big, even if there are copies it's very likely meaningless. Regardless, these are just my opinions and I didn't downvote and your answer is still correct.
– Tas
Mar 22 at 0:01
It is clear, because you cannot store aconst&(without pointer shenanigans that you should not be doing), you can only read and copy from it. This is why I believe your opinion to be misguided, this is the gist of the article I linked to, and why I linked that article.
– Tzalumen
Mar 25 at 17:13
Of course you can store aconst std::vector<float>&, why wouldn't you be able to? You have a reference to the originalvector, which when it changes you receive those changes, but you are unable to change it. So it becomes problematic if a local vector is made, then given to an instance ofMeshwhich outlives thevector. Of course in any given small example things will be fine, but I have professionally encountered issues with passing references. For any given function they're preferred, but into a constructor is a bad idea since the class may store a reference.
– Tas
Mar 25 at 21:58
|
show 4 more comments
I disagree with the last change: taking thevectorby reference won't help, as thevectorwill still get copied intoMesh::positionVertices. One could move it, but otherwise I'd keep it as a copy so the interface reads better (the interface of Mesh says "I copy this" rather than "I might store a reference to this, so ensure you keep it alive for the entire lifetime of me", which makes a difference)
– Tas
Mar 21 at 23:11
@Tas if it's not passed by const ref it will be copied from the original variable into the constructor call, then again into the member variable. I would direct your attention to stackoverflow.com/questions/2139224/…
– Tzalumen
Mar 21 at 23:13
I highly doubt any compiler worth its weight wouldn't omit one of those copies, and for me personally I'd rather my interface be clear a copy is taken, rather than users having to worry about a stored reference. Assuming OP isn't creating 10000Meshwith vectors that are 10000 big, even if there are copies it's very likely meaningless. Regardless, these are just my opinions and I didn't downvote and your answer is still correct.
– Tas
Mar 22 at 0:01
It is clear, because you cannot store aconst&(without pointer shenanigans that you should not be doing), you can only read and copy from it. This is why I believe your opinion to be misguided, this is the gist of the article I linked to, and why I linked that article.
– Tzalumen
Mar 25 at 17:13
Of course you can store aconst std::vector<float>&, why wouldn't you be able to? You have a reference to the originalvector, which when it changes you receive those changes, but you are unable to change it. So it becomes problematic if a local vector is made, then given to an instance ofMeshwhich outlives thevector. Of course in any given small example things will be fine, but I have professionally encountered issues with passing references. For any given function they're preferred, but into a constructor is a bad idea since the class may store a reference.
– Tas
Mar 25 at 21:58
I disagree with the last change: taking the
vector by reference won't help, as the vector will still get copied into Mesh::positionVertices. One could move it, but otherwise I'd keep it as a copy so the interface reads better (the interface of Mesh says "I copy this" rather than "I might store a reference to this, so ensure you keep it alive for the entire lifetime of me", which makes a difference)– Tas
Mar 21 at 23:11
I disagree with the last change: taking the
vector by reference won't help, as the vector will still get copied into Mesh::positionVertices. One could move it, but otherwise I'd keep it as a copy so the interface reads better (the interface of Mesh says "I copy this" rather than "I might store a reference to this, so ensure you keep it alive for the entire lifetime of me", which makes a difference)– Tas
Mar 21 at 23:11
@Tas if it's not passed by const ref it will be copied from the original variable into the constructor call, then again into the member variable. I would direct your attention to stackoverflow.com/questions/2139224/…
– Tzalumen
Mar 21 at 23:13
@Tas if it's not passed by const ref it will be copied from the original variable into the constructor call, then again into the member variable. I would direct your attention to stackoverflow.com/questions/2139224/…
– Tzalumen
Mar 21 at 23:13
I highly doubt any compiler worth its weight wouldn't omit one of those copies, and for me personally I'd rather my interface be clear a copy is taken, rather than users having to worry about a stored reference. Assuming OP isn't creating 10000
Mesh with vectors that are 10000 big, even if there are copies it's very likely meaningless. Regardless, these are just my opinions and I didn't downvote and your answer is still correct.– Tas
Mar 22 at 0:01
I highly doubt any compiler worth its weight wouldn't omit one of those copies, and for me personally I'd rather my interface be clear a copy is taken, rather than users having to worry about a stored reference. Assuming OP isn't creating 10000
Mesh with vectors that are 10000 big, even if there are copies it's very likely meaningless. Regardless, these are just my opinions and I didn't downvote and your answer is still correct.– Tas
Mar 22 at 0:01
It is clear, because you cannot store a
const& (without pointer shenanigans that you should not be doing), you can only read and copy from it. This is why I believe your opinion to be misguided, this is the gist of the article I linked to, and why I linked that article.– Tzalumen
Mar 25 at 17:13
It is clear, because you cannot store a
const& (without pointer shenanigans that you should not be doing), you can only read and copy from it. This is why I believe your opinion to be misguided, this is the gist of the article I linked to, and why I linked that article.– Tzalumen
Mar 25 at 17:13
Of course you can store a
const std::vector<float>&, why wouldn't you be able to? You have a reference to the original vector, which when it changes you receive those changes, but you are unable to change it. So it becomes problematic if a local vector is made, then given to an instance of Mesh which outlives the vector. Of course in any given small example things will be fine, but I have professionally encountered issues with passing references. For any given function they're preferred, but into a constructor is a bad idea since the class may store a reference.– Tas
Mar 25 at 21:58
Of course you can store a
const std::vector<float>&, why wouldn't you be able to? You have a reference to the original vector, which when it changes you receive those changes, but you are unable to change it. So it becomes problematic if a local vector is made, then given to an instance of Mesh which outlives the vector. Of course in any given small example things will be fine, but I have professionally encountered issues with passing references. For any given function they're preferred, but into a constructor is a bad idea since the class may store a reference.– Tas
Mar 25 at 21:58
|
show 4 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55290328%2fcorrect-allocation-for-stdvector%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
off-topic:
std::vector<float> positionVertices-0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f;– bolov
Mar 21 at 22:46
2
I don't see any error in the provided code, it's probably somewhere else. Minimal, Complete, and Verifiable example please.
– HolyBlackCat
Mar 21 at 22:46
There is no obvious problem with the shown code, therefore the problem must be in the code that's not shown.
– Sam Varshavchik
Mar 21 at 22:47
Can you show the constructors for the
MeshandModelclasses? @maniel34– Chase
Mar 21 at 22:53
@Chase Yeah, I put them now into the question. Now, I also think that must be the reason.
– maniel34
Mar 21 at 22:57