我正在使用一个类,该类没有实现赋值运算符,并且禁用了复制构造函数.我可以实例化一个像这样LibraryClass命名的本地实例var:
LibraryClass var(data, (char *)fileName, results);
Run Code Online (Sandbox Code Playgroud)
但我想LibraryClass在我写的类上创建一个实例变量.然后我想在类构造函数中实例化它.像这样的东西:
class MyClass
{
LibraryClass var;
void MyClass();
}
MyClass::MyClass()
{
var = LibraryClass(data, (char *)fileName, results);
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我最终得到了
error: ‘LibraryClass& LibraryClass::operator=(const LibraryClass&)’ is private
LibraryClass& operator=(const LibraryClass& rOther); // no implementation
Run Code Online (Sandbox Code Playgroud)
我已经尝试了所有我能想象的工作,但没有任何工作.我试图做的甚至可能吗?我没有想法,所以任何建议都非常感谢.
编辑
我实际上并没有在构造函数中实例化变量.它发生在一个单独的功能中.我只说构造函数,因为我错误地认为它只是一个简化的假设.我没有意识到初始化列表会解决这个问题.我想回答的主要问题是标题.
如何实例化没有复制构造函数或赋值运算符的类的实例变量?或者是初始化列表是唯一的方法吗?
我不确定我是否正确地提出了我的问题,所以请随时纠正我.我相信:
在初始化列表中初始化相当于
int a = a;
在构造函数中初始化相当于
int a; a = a;
但我仍然无法弄清楚以下输出的原因:
#include <iostream>
using namespace std;
class test
{
int a,b;
public:
/*
test(int a, int b): a(a), b(b) {} //OUTPUT: 3 2
test(int a, int b) { a = a; b = b;} //OUTPUT: -2 1972965730
test(int c, int d) { a = c; b = d;} //OUTPUT: 3 2
Hence it does work without this pointer. Unless the variable names are same
*/
void print() { …Run Code Online (Sandbox Code Playgroud) 抱歉标题非常糟糕,但我真的不知道该怎么写.
我有以下库Neural_Network.h和C++中的类Network:
class Network{
public:
struct Settings
{
uint32_t m_numInputs;
uint32_t m_numHidden;
uint32_t m_numOutputs;
};
Network(Settings const& settings);
}
Run Code Online (Sandbox Code Playgroud)
然后,在Neural_Network.cpp文件中,它按以下方式实现:
Network::Network( Settings const& settings )
: m_numInputs( settings.m_numInputs )
, m_numHidden( settings.m_numHidden )
, m_numOutputs( settings.m_numOutputs )
{
assert( settings.m_numInputs > 0 && settings.m_numOutputs > 0 && settings.m_numHidden > 0 );
InitializeNetwork();
InitializeWeights();
}
Run Code Online (Sandbox Code Playgroud)
我真的不明白为什么我需要在函数名称之后需要:: m_numInputs(settings.m_numInputs),m_numHidden(settings.m_numHidden),m_numOutputs(settings.m_numOutputs).它的目的是什么?
我正在尝试学习一些OOP,我在理解以下程序时遇到问题:
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class A
{
public:
A(int i) : m(i){}
friend class B;
friend void g_afiseaza_m();
private:
int m;
};
class B
{
public:
void afiseaza_m()
{
A a(250);
cout<<"clasa B este prietena cu clasa A"<<endl<<" poate accesa membrul privat A::m"<<endl<<a.m<<endl;
}
};
void g_afiseaza_m()
{
A a(300);
cout<<"functia g_afiseaza_m nu este un membru al clasei A dar este prieten"<<endl<<"poate accesa membrul privat A::m"<<endl<<a.m<<endl;
}
int main()
{
B b;
b.afiseaza_m();
g_afiseaza_m();
getch();
return 0; …Run Code Online (Sandbox Code Playgroud) 我有一个类与另一个类的成员变量:
class MeasurementUnit {
private:
MeasurementMultiplier _multiplier;
Run Code Online (Sandbox Code Playgroud)
实际上我不需要一个默认的构造函数MeasurementMultiplier,因为实际上我会用参数初始化MeasurementMultiplier(a,b,c),我会 - 但不能直接:
C2864: 'MeasurementUnit::_multiplier' :
only static const integral data members can be initialized within a class
Run Code Online (Sandbox Code Playgroud)
所以我需要默认的构造函数,没有它不编译 error: C2512: 'MeasurementUnit' : no appropriate default constructor available
我可以避免需要默认构造函数吗?
class Foo
{
public:
// single parameter constructor, can be used as an implicit conversion
Foo (int foo) : m_foo (foo)
{
}
int GetFoo () { return m_foo; }
private:
int m_foo;
};
Run Code Online (Sandbox Code Playgroud)
m_foo是私有部分中定义的整数,但是m_foo(foo)是什么?看起来像一个功能.
m_foo既是整数又是函数?这是如何运作的?
而Foo(int foo)构造函数正在扩展m_foo函数.
我对以下构造函数感到有点困惑.我知道值键,值和下一个在这里被初始化,但是它们之后的括号让我失望.操作键(键)是否将类型K对象传递给类型K构造函数?这里发生了什么?我正在看的网页就在这里.任何帮助深表感谢.
// Hash node class template
template <typename K, typename V>
class HashNode
{
public:
HashNode(const K &key, const V &value)
: key(key), value(value)
{}
private:
// key-value pair
K key;
V value;
};
Run Code Online (Sandbox Code Playgroud) 我正在从结构C转向OOP C++,:在C++中声明/定义构造函数时,我经常发现" "符号作为运算符的特殊用法.我粗略地理解了这种风格的使用,但是有人用这个构造函数定义向我解释了精确的编程技术.
例如:1
class time_stamp
{
public:
time_stamp(time &t_time)
: m_time(t_time)
{}
~time_stamp()
{
m_time.update(); // as soon as I'm destroyed, update the time
}
private:
time &m_time;
};
Run Code Online (Sandbox Code Playgroud)
例如:2
class threaded_class
{
public:
threaded_class()
: m_stoprequested(false), m_running(false)
{
pthread_mutex_init(&m_mutex);
}
~threaded_class()
{
pthread_mutex_destroy(&m_mutex);
}
/** Some other member declarations */
}
Run Code Online (Sandbox Code Playgroud)
请解释我:在以下2个例子time_stamp(time &t_time) : m_time(t_time){}和下面的代码行中使用" "
threaded_class(): m_stoprequested(false), m_running(false)
{
pthread_mutex_init(&m_mutex);
}
Run Code Online (Sandbox Code Playgroud)