我的程序在尝试为我的阵列分配新内存时保持染色.通过调用添加单词功能.任何帮助都会很棒!
完整代码下载@ http://www.johnsoncomputertechnologies.org/pgm2demo.zip
#include <iomanip> //Standard IO Manipulators Library
#include <algorithm>
#include <iostream> //Standard input/output library
#include <string> //Standard string Library
#include <cassert> //Error handeling Library
#include "pgm2.h"
using namespace std;
dictionary::dictionary()
{
wordptr = new value_type[DICTONARY_CAPACITY];
deffptr = new value_type[DICTONARY_CAPACITY];
used = 0;
}
dictionary::dictionary(const dictionary& other)
{
value_type *wordptr;
value_type *deffptr;
wordptr = new value_type[other.capacity];
deffptr = new value_type[other.capacity];
used = other.used;
capacity = other.capacity + 1;
copy(wordptr, other.wordptr + used, other.wordptr);
copy(deffptr, other.deffptr + used, other.deffptr);
} …Run Code Online (Sandbox Code Playgroud) 根据我的情况1使用复制赋值运算符,因此输出应该是0 68但0 87在情况2中它87 87是正常的.
#include <iostream>
using namespace std;
class numbered
{
static int un;
public:
int a;
numbered (): a(un) {un++;}
numbered(const numbered & n) : a(87){}
numbered & operator=(const numbered) { a=68; return *this; }
};
int numbered::un=0;
void f(numbered s){ cout<<s.a;}
int main()
{
numbered a, b=a;
cout << a.a << b.a; //case 1
f(a);f(b); //case 2
return 0;
}
Run Code Online (Sandbox Code Playgroud) 为什么我们通过引用传递类的对象.当我删除&符号(&)时出现以下错误.
"Copy constructor of class A may not have parameter of type A"
Run Code Online (Sandbox Code Playgroud)
这是什么意思?可能是编译器没有考虑给定一个复制构造函数并使用默认值.如果是这种情况,为什么要调用默认值.简而言之,为什么我们使用&符号会发生什么?如果我们不这样做.
class A
{
public:
A()
{
}
A(A& tmp)
{
id = 2*tmp.id;
}
public:
int id;
};
int main()
{
A obj;
obj.id = 10;
A obj1(obj);
cout << obj1.id;
}
Run Code Online (Sandbox Code Playgroud) class Myinteger(){
public:
Myinteger( int len ); // simple constructor
Myinteger( const Myinteger &obj); // copy constructor
~Myinteger(); // destructor
private:
int *ptr;
}
Myinteger::Myinteger(const Myinteger &obj) {
cout << "Copy constructor allocating ptr." << endl;
ptr = new int;
*ptr = *obj.ptr; // copy the value
}
int main(){
Myinteger obj1(10);
Myinteger obj2(20);
obj1=obj2;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
将obj2分配给obj1时不调用复制构造函数,我确认它是"复制构造函数分配ptr".没有打印到控制台,
因此,如果在将obj2分配给obj1时,如果没有调用复制构造函数在上述情况下调用哪个方法,那么也要告诉在哪些情况下调用类的复制构造函数.
我有一个A类.在这个类中它包含一个指向另一个A的指针.
class A
{
A* sub = NULL;
};
Run Code Online (Sandbox Code Playgroud)
我想有一个默认指针的空构造函数NULL,以及另一个传递指针/引用的构造函数.第二个构造函数将参数复制到一个new A()对象中,并将sub从参数传递给它自己.
现在的班级:
class A
{
A* sub = NULL
A(A* source)
{
this->sub = new A(*source);//copy the source 'A'
// we now have a copy of "source" and all of its children
// but to prevent the "source" from deleting our new
// children (destructor deletes children recursively),
// "source"s children are disconnected from "source"
source->sub = NULL;
// this invalidates sub, but that is desired …Run Code Online (Sandbox Code Playgroud) 我试图自己为 my_vec 编写一个迭代器:
#define BEGIN true
#define END false
#include <vector>
#include <iostream>
template<typename Container>
class my_vec {
private:
class iterator {
const my_vec *this_vec;
using iterator_type = typename std::vector<std::pair<int, const Container&>>::const_iterator;
iterator_type itr;
public:
iterator(const my_vec &s, bool state) :
this_vec(&s) {
if (state == BEGIN) {
itr = s.v.begin();
} else { /*(state==END)*/
itr = s.v.end();
}
}
iterator& operator++() {
itr++;
return *this;
}
std::pair<int, const Container&> operator*() const {
return std::make_pair(1, this_vec->dog);
}
bool operator!=(iterator other) …Run Code Online (Sandbox Code Playgroud) 我用 C++20 编写了一个无锁且线程安全的环形队列,到目前为止它可以工作。唯一不完美的是它必须有两个enque()方法,一个接受对左值的 const 引用作为参数,另一个接受对右值的引用,以便将右值移入队列而不是再次构造。
之前版本的代码如下,只是一个骨架,进行简化:
\ntemplate <typename T>\nclass RingQue_t {\npublic:\n explicit RingQue_t( size_t capacity );\n ~RingQue_t();\n bool deque( T& dest_ ) { return true; };\n\n // If we offer a const ref, the compiler will select this one\n bool enque( const T& src_ ) {\n // a lot of same codes goes here for a same logic...\n\n new( _buff + _tail ) T( src_ );\n };\n\n // If we offer a rvalue ref, the compiler …Run Code Online (Sandbox Code Playgroud) c++ copy-constructor rvalue-reference move-constructor perfect-forwarding
哪个代码更适合使用:初始化字符串?
bool flag = /*function call...*/
string str = "abc";
if(flag)
str = "bcd";
Run Code Online (Sandbox Code Playgroud)
要么
string str;
if(flag)
str = "bcd";
else
str = "abc";
Run Code Online (Sandbox Code Playgroud)
要么
string str("abc");
if(flag)
str = "bcd";
Run Code Online (Sandbox Code Playgroud)
提前致谢。
可以std::string在C++ 11中复制构造函数吗?
(Stackoverflow说我的问题不符合其质量标准,似乎它只是想要更多的散文,所以这里有一些虚拟文本.)
这是本书的一个例子:
template <class T>
class stack
{
public:
stack();
stack(const stack&);
stack & operator=(const stack&);
~stack();
T& top();
void push(const T&);
void pop();
//few more functions
}
Run Code Online (Sandbox Code Playgroud)
我有以下问题:
为什么top的返回类型不是堆栈&为什么重载赋值的参数不是T&?
Rgds,Softy
c++ ×10
copy-constructor ×10
c++11 ×2
constructor ×1
exception ×1
iterator ×1
stack ×1
stdstring ×1
string ×1