copy-constructor

called object not a function c

拜拜、爱过 提交于 2019-12-25 00:17:34
问题 Good morning SO- this ones been cooking my noodle for a bit (also, it's very hard to google about the c programming language I've noticed...) void prepareFrames(PDouble prep){ int gap = prep->gap; printf("Gap is %d\n", gap); prep->intFrames = (PFrame*)malloc(gap*sizeof(PFrame)); PFrame copyFrame = prep->first; int i; for(i=0; i < gap; i++){ prep->intFrames[i] = (PFrame)malloc(sizeof(Frame)); copyFrame(prep->intFrames[i], prep->first, i); //LINE 189 } } void copyFrame(PFrame new, PFrame copy,

Neither copy nor move constructor called [duplicate]

允我心安 提交于 2019-12-24 21:15:31
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Why copy constructor is not called in this case? What are copy elision and return value optimization? Can anybody explain to me why the following program yields output "cpy: 0" (at least when compiled with g++ 4.5.2): #include<iostream> struct A { bool cpy; A() : cpy (false) { } A (const A & a) : cpy (true) { } A (A && a) : cpy (true) { }; }; A returnA () { return A (); } int main() { A a ( returnA () ); std:

Copy constructor for class that has member without default constructor in C++

淺唱寂寞╮ 提交于 2019-12-24 12:27:15
问题 I have a class: class Geometry{ std::vector<Subset *> subsets; int verticesCount; ... }; I want to add a copy constructor, so I can make a deep copy of that object (with own subsets , NOT only own pointers to same subsets ). I have tried this: Geometry & Geometry::operator=(const Geometry &other){ verticesCount = other.verticesCount; Subset * subset = 0; for(unsigned int i=0; i<other.subsets.size(); i++){ subset = new Subset(); subsets.push_back(subset); } ... return *this; } The problem is

How can I create a Lazy C++ template class that handles types with no default constructor?

£可爱£侵袭症+ 提交于 2019-12-24 11:58:42
问题 Deleting this question in favor the following; an answer to which now handles classes with no default constructor: How to abstract lazy initialization in C++? In a nutshell, the code uses placement new/delete. See http://en.wikipedia.org/wiki/Placement_syntax for details... 回答1: Just use boost::optional<T> instead of pair of your members m_bInitialized and m_value . Probably you could just use boost::optional<T> instead of your template class Lazy ... If you really want to make it in your own

Copy constructor calls destructor c++

安稳与你 提交于 2019-12-24 09:18:27
问题 I have a test class of my to make my own string functions. I have a problem with the copy destructor. I have 2 strings: s1 and s2. I call the function s3 = s1 + s2; It first calls the operator+ function and when it's finished it calls the destructor. Because of this the string object in the operator= function is empty. How can I fix this? Destructor: String::~String() { if (this->str) delete[] str; str = NULL; len = 0; } Copy Constructor: String::String(const String& string) { this->len =

C++: Copy constructor: Use getters or access member vars directly?

陌路散爱 提交于 2019-12-24 00:58:23
问题 I have a simple container class with a copy constructor. Do you recommend using getters and setters, or accessing the member variables directly? public Container { public: Container() {} Container(const Container& cont) //option 1 { SetMyString(cont.GetMyString()); } //OR Container(const Container& cont) //option 2 { m_str1 = cont.m_str1; } public string GetMyString() { return m_str1;} public void SetMyString(string str) { m_str1 = str;} private: string m_str1; } In the example, all code is

copy construtor called extra

我是研究僧i 提交于 2019-12-23 18:04:03
问题 I have a program which is showing weird behaviour #include <cstdlib> #include <iostream> using namespace std; class man{ int i ; public: man(){ i=23; cout << "\n defaul constructir called\t"<< i<<"\n"; } man (const man & X) { i = 24; cout << "\n COPY constructir called\t"<< i<<"\n"; } man & operator = (man x ) { i = 25; cout << "\n = operator called\t"<< i<<"\n"; return *this; } }; int main(int argc, char *argv[]) { man x; cout <<"\n ----------\n"; man y = x; cout <<"\n ----------\n"; x=y;

Double free of child object after using the copy constructor

久未见 提交于 2019-12-23 14:19:54
问题 I am having trouble figuring out why (it seems like) an object is being destructed twice. If i create a object of a class (B) which contains an object of another class (A) and i copy this object. the copied object is destructed twice. Altough it looks like this. I am unable to figure out this output. I have created the following (minimum?) example which seems to trigger my issue: #include <stdio.h> #include <stdint.h> template <class T> class A { public: A() { myCtr = ++ctr; printf("class A

Double free of child object after using the copy constructor

限于喜欢 提交于 2019-12-23 14:19:09
问题 I am having trouble figuring out why (it seems like) an object is being destructed twice. If i create a object of a class (B) which contains an object of another class (A) and i copy this object. the copied object is destructed twice. Altough it looks like this. I am unable to figure out this output. I have created the following (minimum?) example which seems to trigger my issue: #include <stdio.h> #include <stdint.h> template <class T> class A { public: A() { myCtr = ++ctr; printf("class A

Copy constructor using reflection [closed]

不羁岁月 提交于 2019-12-23 10:14:29
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . I have a Base class with 100 fields and a Derived class with 2 more fields. I want to have all the 100 fields accessible in the Derived class by calling the getters in the Base class, so that's why I'm using inheritance and not composition. In Derived I want to have a constructor which initializes everything