返回变量时,一步一步地发生了什么.我知道如果它是内置的并且适合,它会被抛入rax/eax/ax.当它不适合和/或不是内置时会发生什么?更重要的是,是否有保证的复制构造函数调用?
编辑:析构函数怎么样?那被称为"有时","总是"还是"从不"?
#include <iostream>
using namespace std;
class tester {
public:
int a;
tester( int x ) {
a = x;
}
tester( tester &t ) {
cout << t.a;
}
};
int main() {
tester t(10);
tester t_1(t);
}
output : 10
Run Code Online (Sandbox Code Playgroud)
在复制构造函数的定义中t引用了什么?从main传入t参数时t_1,它的地址被存储在&t复制构造函数的表单中.什么t.a意思?
我正在尝试在我的List类中创建一个动态数组,该数组将从大小为2开始,当您使用Insert方法插入值时,它将检查是否有足够的空间,否则它将调整数组的大小大小+ 2 ...问题是它崩溃VS正在抱怨堆的损坏.另外我认为我的拷贝构造函数没有被调用,因为cout没有显示:
list.h文件:
class List
{
public:
// DEFAULT Constructor
List();
// Deconstructor to free memory allocated
~List();// Prevent memory leaks
// COPY Constructor for pointers
List(const List& value);// copy constructor
//Modification methods
void Insert(const int);
// User ACCESS methods
void display(void) const;
private:
int size;// MAX size of the array
int count;// current number of elements in the dynamic array
protected:
int *intptr;// Our int pointer
};
Run Code Online (Sandbox Code Playgroud)
list.cpp实现文件:
#include "list.h" // Include our Class defintion
#include <iostream>
using …Run Code Online (Sandbox Code Playgroud) OOP有些新东西(即C程序员转换为C++)并且无法弄清楚为什么我的背包类中的数据成员是空的.我向我的背包传递了一系列药水,但数据成员说mType =""(即什么都没有).
我以前从来没有觉得这在节目中输了.开始讨厌OOP(即开玩笑......但这非常令人沮丧).
main.cpp中
#include <iostream>
#include "rogue.h"
#include "weapon.h"
#include "backpack.h"
#include "potion.h"
#include "coinpouch.h"
int main()
{
Potion myFavoritePotions[5];
myFavoritePotions[0].setName("love");
myFavoritePotions[1].setName("hate");
myFavoritePotions[2].setName("shrink");
myFavoritePotions[3].setName("grow");
myFavoritePotions[4].setName("disappear");
BackPack myFavoriteBackPack(myFavoritePotions);
Weapon myFavoriteWeapon("AK-47");
Weapon mySecretWeapon("Me-262");
Weapon myLeastFavoriteWeapon("Luger");
CoinPouch myFavoritePurse(6,5,4,3);
Rogue myFavoriteRogue("Cynic", myFavoriteWeapon, mySecretWeapon, myFavoriteBackPack, myFavoritePurse);
mySecretWeapon = myFavoriteWeapon;
myFavoriteRogue.setOffHand(myLeastFavoriteWeapon);
//std::cout << myFavoriteRogue.getOffHand();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
potion.cpp
#include <iostream>
#include "potion.h"
//Manager function definitions
//Default constructor
Potion::Potion()
{}
//Constructor
Potion::Potion(std::string name)
:mName(name)
{
std::cout << "Potion's constructor " << std::endl;
}
//Destructor
Potion::~Potion()
{ …Run Code Online (Sandbox Code Playgroud) 我有一个类A有vector<Object> object_list_一个成员变量。
我知道如果我有一个指针向量,我将需要编写一个特定的复制构造函数来实现深度复制。但是,在这种情况下,由于我没有指针,因此在复制 type 的对象时A,默认的复制构造函数也会自动调用Object或每个元素的复制构造函数,或者object_list_我是否必须编写自己的复制构造函数来执行此操作?
我需要为我的类创建一个复制构造函数,Immagine如下所示:
Immagine::Immagine(Immagine& i)
{
...
}
Run Code Online (Sandbox Code Playgroud)
很明显,当我试着打电话时,我有类似的东西:
error: invalid initialization of non-const reference of type ‘Immagine&’ from an rvalue of type ‘Immagine’因为我会声明它:
Immagine::Immagine(const Immagine& i)
{
...
}
Run Code Online (Sandbox Code Playgroud)
但我无法做到这一点,因为,为了对一个Immagine成员对象进行初始化,我使用了一个函数
Immagine::Immagine(const Immagine& i)
{
dlib::array2d<dlib::rgb_pixel>& r=i.v; //v is a member of type dlib::array2d<dlib::rgb_pixel>
dlib::assign_image(this->dlib_immagine,r);
}
Run Code Online (Sandbox Code Playgroud)
功能dlib::assign_image(dst,src)是封装功能,并复制src到dst,但没有被声明const为src参数,所以如果我声明i作为const我得到一个错误......我该如何解决这个问题?
我正在查看关于Cascade Classifiers的OpenCV教程,并且碰巧看到了这种语法:
std::vector<Rect> faces;
Mat frame_gray;
Mat faceROI = frame_gray( faces[i] );,
Run Code Online (Sandbox Code Playgroud)
在frame_gray实例化和faceROI实例化之间还有一些其他代码.我的问题是 - faceROI实例化行正在做什么/它是如何工作的?它看起来像复制构造函数用法,但faces [i]参数让我感到困惑.
http://docs.opencv.org/trunk/db/d28/tutorial_cascade_classifier.html - 教程 http://docs.opencv.org/3.1.0/d3/d63/classcv_1_1Mat.html#af1d014cecd1510cdf580bf2ed7e5aafc - Mat类的文档
我正在阅读一篇关于rvalue-references的文章,但我对下面的摘录感到困惑:
X foo();
X x;
// perhaps use x in various ways
x = foo();
Run Code Online (Sandbox Code Playgroud)
这里,X是一些用户定义的类型.
我知道X foo();会调用X的构造函数,我很确定X x;不会.
但是,这条线x = foo();意味着什么?是否有一个名为?的构造函数?什么会x = foo;做?
尽我所能,我无法弄清楚要查找的正确单词以找出此代码的含义.
为什么抛出作为引用的异常调用复制构造函数?
struct Error
{
Error() {}
Error(const Error&) = delete;
};
int main()
{
Error& error = *new Error;
throw error;
}
Run Code Online (Sandbox Code Playgroud)
编译错误:
error: declared here
Error(const Error&) = delete;
Run Code Online (Sandbox Code Playgroud)
投掷指针时不会发生这种情况:
int main()
{
Error* error = new Error;
throw error;
}
Run Code Online (Sandbox Code Playgroud)
还行吧.
当我定义一个类时,如果我需要深拷贝,我需要定义我自己的拷贝构造函数。那么,是否也需要定义赋值运算符呢?如果跳过,赋值是否做浅拷贝?
c++ ×10
copy-constructor ×10
constructor ×2
oop ×2
arrays ×1
c++11 ×1
const ×1
dynamic ×1
exception ×1
member ×1
opencv ×1
return-value ×1
throw ×1
vector ×1