我遇到了一个代码片段,并认为它会调用copy-constructor,但相比之下,它只是调用了普通的构造函数.下面是代码
#include <iostream>
using namespace std;
class B
{
public:
B(const char* str = "\0")
{
cout << "Constructor called" << endl;
}
B(const B &b)
{
cout << "Copy constructor called" << endl;
}
};
int main()
{
B ob = "copy me";
return 0;
}
Run Code Online (Sandbox Code Playgroud) 现在,我知道这是一个常见的问题,但我还没有真正找到答案.这真的是一个关于标准的问题.我正在研究一个涉及遗传算法的项目.但是,当涉及到返回矢量时,我遇到了瓶颈.有没有"适当的"方法来做到这一点.通常我使用动态分配的数组,并返回一个指向新创建的数组的指针.
obj* func(obj* foo);
Run Code Online (Sandbox Code Playgroud)
这样,一切都很有效,没有数据复制.有没有相当于用矢量这样做?这个向量中有对象,所以按值返回它会很慢.是通过引用传递"结果"向量的唯一解决方案吗?
void func(vector<obj> &input, vector<obj> &result);
Run Code Online (Sandbox Code Playgroud)
并且,在备注以供将来参考时,在动态分配的数组上使用向量或其他STL容器是标准做法吗?动态分配的数组是否仅用作设计容器的低级工具?它们只是过去的遗物吗?
众所周知,由于作用域,从C++中的函数返回局部变量是不安全的.在Effective C++第三版中,Scott Meyers在第21页的第21项中讲述了这个问题.然而,最后他说,正确的决定将是:
inline const Rational operator*(const Rational& lhs, const Rational& rhs) {
return Rational(lhs.n * rhs.h, lhs.d * rhs.d);
}
Run Code Online (Sandbox Code Playgroud)
这不是一个坏习惯,这个功能不安全吗?
UPD:谢谢大家的解释.
我有一个函数将shared_ptr返回给const对象.返回由operator new返回的指针构造的shared_ptr,但返回该指针会导致编译错误:
Error 3 error C2664: 'std::shared_ptr<_Ty>::shared_ptr(std::nullptr_t)' : cannot convert parameter 1 from 'script::float_data *' to 'std::nullptr_t' c:\xxx\value.cpp 59
Run Code Online (Sandbox Code Playgroud)
以下是导致错误的代码:
shared_ptr< const data > float_data::operator + ( shared_ptr< const data > rhs ) const
{
int rhs_as_int; float rhs_as_float;
switch( rhs->to_numeric( rhs_as_int, rhs_as_float ) )
{
case E_INT:
return new float_data( val + rhs_as_int );
case E_FLOAT:
return new float_data( val + rhs_as_float );
}
}
Run Code Online (Sandbox Code Playgroud)
这些课程是:
class data
{
public:
enum type
{
E_INT,
E_FLOAT,
E_STRING
};
public:
virtual …Run Code Online (Sandbox Code Playgroud) 在这个例子中,移动语义是如何工作的:
struct test {
int ii[10];
int i;
};
test f() {
test a;
std::cout << "[1] " << &a << std::endl;
return a;
}
int main()
{
test x (f());
std::cout << "[2] " << &x << std::endl;
test x1 (std::move(x));
std::cout << "[3] " << &x1;
}
Run Code Online (Sandbox Code Playgroud)
输出:
[1] 0x7fff50ac70c0
[2] 0x7fff50ac70c0
[3] 0x7fff50ac70f0
Run Code Online (Sandbox Code Playgroud)
为什么x是使用f()的返回值构造的,但是x1的地址与x不同?
编辑
struct test {
std::string s;
}
[...]
std::cout << (*void)&x.s[0];
Run Code Online (Sandbox Code Playgroud)
我想最终我明白了.现在地址是一样的.
如果我修改赋值opreator以便它返回一个对象A而不是对对象A的引用,那么就会发生一些有趣的事情.
每当调用赋值运算符时,都会在之后调用复制构造函数.为什么是这样?
#include <iostream>
using namespace std;
class A {
private:
static int id;
int token;
public:
A() { token = id++; cout << token << " ctor called\n";}
A(const A& a) {token = id++; cout << token << " copy ctor called\n"; }
A /*&*/operator=(const A &rhs) { cout << token << " assignment operator called\n"; return *this; }
};
int A::id = 0;
A test() {
return A();
}
int main() {
A a;
cout << "STARTING\n";
A …Run Code Online (Sandbox Code Playgroud) 编辑
*请提出这个主题,因为我在这个论坛上不能再提问了.编程就是我的生活,我因为自动禁止而陷入困境.谢谢(或者我需要主持人的帮助来解决这个问题*
我是c ++的初学程序员,我想基本上返回一个std::vector
当我调试我的代码时,我得到函数调用缺少参数列表.这是我的简单代码
谢谢你的帮助
#include "stdafx.h"
#include <vector>
#include <iostream>
static std::vector<int> returnStaticVector();
static std::vector<int> returnStaticVector(){
std::vector<int> vectorInt = std::vector<int>();
vectorInt.push_back(0);
return vectorInt;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<int> a = std::vector<int>();
a = returnStaticVector(); // Compile , but error when I try to access to the size of the std::vector
//int size = a.size; //error C3867: 'std::vector<int,std::allocator<_Ty>>::size': function call missing argument list; use '&std::vector<int,std::allocator<_Ty>>::size' to create a pointer to member
//int size = &a.size; …Run Code Online (Sandbox Code Playgroud) 我需要一个char*我的文件名.它应该是这样的:cards/h11.bmp
我有一个函数,我从各种SO文章拼凑而成:
char* getFileName(int* pc1_no, char* suite)
{
int number;
char pCard1[80];
strcpy_s(pCard1, "cards/");
strcat_s(pCard1, suite);
number = *pc1_no;
cout << number << endl;
string str = to_string(number);
char const *pchar = str.c_str();
strcat_s(pCard1, pchar);
strcat_s(pCard1, ".bmp");
return pCard1;
}
Run Code Online (Sandbox Code Playgroud)
当然,返回垃圾.我没有得到指针值.我很确定我用指针犯了一个愚蠢的错误.提前致谢.
我需要解释以下行为:
#include <iostream>
#include <memory>
#include <vector>
struct A {
std::string s = "foo";
std::weak_ptr<A> h;
std::shared_ptr<A> && getR() {
return std::move(h.lock());
}
std::shared_ptr<A> getL() {
return h.lock();
}
};
std::vector< std::shared_ptr<A> > storage;
std::vector< std::weak_ptr<A> > accountant;
void store(std::shared_ptr<A> && rr) {
std::cout << "store '" << rr->s << "' uses: " << rr.use_count() << std::endl;
storage.push_back(std::move(rr));
}
int main() {
// create keeper of A
auto keeper = std::make_shared<A>();
keeper->s = "bar";
// store weak_ptr-type handle with accountant …Run Code Online (Sandbox Code Playgroud) std::string my_func(){
return std::string("...");
}
Run Code Online (Sandbox Code Playgroud)
std::string可以替换为std::vector或其他任何东西.一般来说,我怎么知道rv被移动而不是被复制?
我有这个代码
class MyString {
public:
MyString();
MyString(const char*);
MyString(const String&);
MyString(String&&) noexcept;
...
};
String::String()
{
std::cout << "default construct!" <<std::endl;
}
String::String(const char* cb)
{
std::cout << "construct with C-char!" <<std::endl;
...
}
String::String(const String& str)
{
std::cout << "copy construct!" <<std::endl;
...
}
String::String(String&& str) noexcept
{
std::cout << "move construct!" <<std::endl;
...
}
Run Code Online (Sandbox Code Playgroud)
在main()
MyString s1(MyString("test"));
Run Code Online (Sandbox Code Playgroud)
我以为结果会是这样的:
用 C-char 构建!<---- 由 MyString("test")
移动构造调用!<---- 由 s1(...) 调用
但我得到的是这样的:
用 C-char 构建!<---- 可能由 MyString() 调用
我想到的步骤
MyString("test")使用构造函数来构造右值 …当我谈论Go时,我正在谈论gc编译器实现.
据我所知,Go执行逃逸分析.Go代码中经常出现以下习语:
func NewFoo() *Foo
Run Code Online (Sandbox Code Playgroud)
转义分析会注意到Foo转义NewFoo并在堆上分配Foo.
这个函数也可以写成:
func NewFoo(f *Foo)
Run Code Online (Sandbox Code Playgroud)
并将被用作
var f Foo
NewFoo(&f)
Run Code Online (Sandbox Code Playgroud)
在这种情况下,只要f不在其他地方转义,就可以在堆栈上分配f.
现在来看我的实际问题.
编译器是否可以优化每个foo() *Foo进程foo(f *Foo),甚至可能在多个级别进行优化,其中Foo在每个级别中返回?
如果不是,这种方法在哪种情况下会失败?
先感谢您.
c++ ×11
c++11 ×4
constructor ×2
return ×2
shared-ptr ×2
vector ×2
c++14 ×1
go ×1
local ×1
optimization ×1
reference ×1
return-value ×1
weak-ptr ×1