请考虑以下声明: -
Class A a = b;//Where is b is existing object of class A.
Run Code Online (Sandbox Code Playgroud)
A类有复制构造函数和赋值运算符重载(实现).在这种情况下,将调用上述语句,复制构造函数或赋值运算符或两者?
我正在为一个赋值创建一组派生类.我被指示使用char数组(c-strings).当我编译时,我不断收到错误:
Homework11.cpp: In function âint main()â:
Homework11.cpp:72: error: conversion from âchar [10]â to non-scalar type âBusinessâ requested
Homework11.cpp:73: error: conversion from âchar [10]â to non-scalar type âBusinessâ requested
Homework11.cpp:74: error: conversion from âchar [10]â to non-scalar type âAccountâ requested
Homework11.cpp:75: error: conversion from âchar [10]â to non-scalar type âAccountâ requested
Run Code Online (Sandbox Code Playgroud)
我相当肯定我的问题源于我尝试将实例变量Name设置为发送的参数.这是我的代码,其中包含注释,我相信问题可能存在.
#include <iomanip>
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
class Person{
public:
Person() {}
Person(char theName[]) {strcpy(name,theName);}
void getName(char theName[]) // I think the problem may be …Run Code Online (Sandbox Code Playgroud) 我有以下代码片段.有谁知道为什么在主函数中没有为所有情况调用此移动构造函数?为什么要编译呢?赋值运算符是私有的吗?链接:http://ideone.com/bZPnyY
#include <iostream>
#include <vector>
class A{
public:
A(int i){
std::cout << "Constructor "<< i <<std::endl;
for(int l = 0; l<i;l++){
vec.push_back(l);
}
};
A(A && ref): vec(std::move(ref.vec))
{
std::cout << "Move constructor"<<std::endl;
}
A & operator=(A && ref){
if(this != &ref){
vec = std::move(ref.vec);
}
std::cout << "Move assignment"<<std::endl;
return *this;
}
std::vector<int> vec;
private:
A(const A & ref);
A(A & ref);
A & operator=(A & ref);
};
A makeA(){
A a(3);
return a;
}
int …Run Code Online (Sandbox Code Playgroud) class Test
{
public:
Test(int i) { cout<<"constructor called\n";}
Test(const Test& t) { cout<<" copy constructor called\n";}
};
class Test1
{
public:
Test1(int i) { cout<<"constructor called\n";}
explicit Test1(const Test1& t) { cout<<" copy constructor called\n";}
};
int main()
{
Test t(0);
Test u = 0;
//Test1 t1(0); Line 1
//Test1 u1 = 0; Line 2
}
Run Code Online (Sandbox Code Playgroud)
我观察到不同的输出。情况 1:当第 1 行和第 2 行被注释时,o/p 为:构造函数调用 构造函数调用
情况 2:当第 1 行和第 2 行取消注释时:则编译错误
有人可以解释一下输出及其原因吗?也有人可以告诉operator=是否实际上最终调用了复制构造函数。
c++ constructor explicit copy-constructor copy-initialization
我正在学习构造函数和析构函数,并且已经学会了三个规则.
我现在正在播放tutorialspoint中的一个小例子.我注意到该示例没有赋值运算符,但代码以某种方式运行良好.例如,Line a(b)当我改变a中的内容时,例如*(a.ptr),*(b.ptr)不会改变.
我还写了一个赋值运算符(注释),代码也可以.
现在我很困惑.在某些情况下,似乎只有复制构造函数就足够了.任何人都可以对此发表评论,以帮助我更好地了解调用复制构造函数所涉及的内存分配机制吗?
谢谢!
#include <iostream>
using namespace std;
class Line
{
public:
int getLength( void );
Line( int len ); // simple constructor
Line( const Line &obj); // copy constructor
~Line(); // destructor
void doubleLength(void);
Line &operator=(const Line &);
private:
int *ptr;
};
// Member functions definitions including constructor
Line::Line(int len)
{
cout << "Normal constructor allocating ptr" << endl;
// allocate memory for the pointer;
ptr = new int; //simply …Run Code Online (Sandbox Code Playgroud) 标题说明了一切.但是,请string作为任何课程的占位符.
std::string s1("hello"); // construct from arguments
std::string s2 = "hello"; // ???
std::string s3; // construct with default values
s3 = "hello"; // assign
Run Code Online (Sandbox Code Playgroud)
我想知道声明s2是否与for s1或for 相同s3.
c++ initialization variable-assignment copy-constructor assignment-operator
我正在为我创建的一个类编写一个复制赋值运算符,我使用以前的帖子作为指导: 什么是三法则?
我对这个人解释的一个方面有点困惑。
这是他们的课:
class person
{
char* name;
int age;
};
Run Code Online (Sandbox Code Playgroud)
这是我用作参考的复制赋值运算符定义(至少提供了 2 个):
// 2. copy assignment operator
person& operator=(const person& that)
{
char* local_name = new char[strlen(that.name) + 1];
// If the above statement throws,
// the object is still in the same state as before.
// None of the following statements will throw an exception :)
strcpy(local_name, that.name);
delete[] name;
name = local_name;
age = that.age;
return *this;
}
Run Code Online (Sandbox Code Playgroud)
我发现令人困惑的是,为什么他们包括这条线delete[] name;?
这是他们提供的另一个示例:
person& operator=(const …Run Code Online (Sandbox Code Playgroud) 我是 C++ 新手,正在尝试理解一些东西。我的 main.cpp 中有这段代码:
Radio r = Radio("PSR", 100.8);
Run Code Online (Sandbox Code Playgroud)
或该代码:
Radio r("PSR", 100.8);
Run Code Online (Sandbox Code Playgroud)
两者似乎都有效并且做同样的事情。那么有什么区别呢?
c++ constructor initialization variable-initialization visual-studio
我一直认为对于与类类型不匹配的类型 T 的直接初始化和复制初始化是绝对相等的。但我似乎误会了。如果我复制初始化(使用=),下面的代码不会编译,并且只有当我通过括号()直接初始化时才编译(在任何情况下,代码在终止时都不起作用,但这是一个不同的故事,与这个问题)。
#include <future>
#include <cstdio>
int main()
{
/* This doesn't compile */
// std::packaged_task<int()> foo = []() -> int {
// return 10;
// };
/* This works */
std::packaged_task<int()> foo([]() -> int {
return 10;
});
auto fut = foo.get_future();
foo();
auto a = fut.get();
printf("a == %d\n", a);
}
Run Code Online (Sandbox Code Playgroud)
错误:
<source>: In function 'int main()':
<source>:8:37: error: conversion from 'main()::<lambda()>' to non-scalar type 'std::packaged_task<int()>' requested
8 | std::packaged_task<int()> foo = []() -> …Run Code Online (Sandbox Code Playgroud) 我正在阅读一篇关于有效使用的文章auto_ptr.在那里,以下代码被建议为正确的代码段:
// Example 10(c): Correct (finally!)
//
auto_ptr<String> f()
{
auto_ptr<String> result = new String;
*result = "some value";
cout << "some output";
return result; // rely on transfer of ownership;
// this can't throw
}
Run Code Online (Sandbox Code Playgroud)
但据我所知,赋值运算符auto_ptr只接受另一个auto_ptr作为rhs- 以避免意外误用.那么,下面的一行是文章中的拼写错误,还是它真的能够起作用?
auto_ptr<String> result = new String;
Run Code Online (Sandbox Code Playgroud)