实现一种机制,允许JavaScript ECMA6中的构造函数重载
主题为什么JavaScript ES6不支持多构造函数类?虽然类似的不一样.另一个主题仅关注使用旧 ECMAScript版本的构造函数重载,而本主题则侧重于ECMA6.如果您正在寻找更新的答案,那就是这个地方.
我有一个带有给定构造函数的JavaScript类,我希望用户在实例化对象时能够拥有不同的构造函数.我假装的一个例子如下:
const DEFAULT_WHEEL_NUMBER = 4;
const DEFAULT_COLOR = "black";
const DEFAULT_NAME = "myCar";
class Car{
constructor(numberWheels, aName, aColor){
this.wheelsNum = numberWheels;
this.name = aName;
this.color = aColor;
}
constructor(aName){
this(DEFUALT_WHEEL_NUMBER, aName, DEFAULT_COLOR);
}
constructor(){
this(DEFUALT_WHEEL_NUMBER, DEFAULT_NAME, DEFAULT_COLOR);
}
}
Run Code Online (Sandbox Code Playgroud)
在此代码中,用户有三个可以使用的构造函数,每个构造函数都使用不同数量的参数.一个用法示例如下:
var car1 = new Car(3, "tricicle-car", "white");
var car2 = new Car("Opel"); //creates black car with 4 wheels called Opel
var car3 = new Car(); //creates a black car, …Run Code Online (Sandbox Code Playgroud) javascript constructor overloading constructor-overloading ecmascript-6
我有一个像这样的课:
class Test{
public:
Test(string value);
Test(bool value);
};
Run Code Online (Sandbox Code Playgroud)
如果我创建这样的对象:
Test test("Just a test...");
Run Code Online (Sandbox Code Playgroud)
bool构造函数被调用!
谁知道为什么?
谢谢
我有一小段C++ 11代码,g ++(4.7或4.8)拒绝编译声称对B2 b2a(x,{P(y)})的构造函数的调用是不明确的.Clang ++对这段代码很满意,但拒绝编译B2 b2b(x,{{P(y)}}),g ++非常乐意编译!
两个编译器都非常满意B1构造函数,其中{...}或{{...}}作为参数.任何C++语言律师都可以解释哪个编译器是正确的(如果有的话)以及发生了什么?代码如下:
#include <initializer_list>
using namespace std;
class Y {};
class X;
template<class T> class P {
public:
P(T);
};
template<class T> class A {
public:
A(initializer_list<T>);
};
class B1 {
public:
B1(const X&, const Y &);
B1(const X&, const A<Y> &);
};
class B2 {
public:
B2(const X &, const P<Y> &);
B2(const X &, const A<P<Y>> &);
};
int f(const X &x, const Y y) {
B1 b1a(x, {y});
B1 b1b(x, {{y}}); …Run Code Online (Sandbox Code Playgroud) 今天我发现我不理解C++构造函数优先级规则.
请参阅以下模板 struct wrapper
template <typename T>
struct wrapper
{
T value;
wrapper (T const & v0) : value{v0}
{ std::cout << "value copy constructor" << std::endl; }
wrapper (T && v0) : value{std::move(v0)}
{ std::cout << "value move constructor" << std::endl; }
template <typename ... As>
wrapper (As && ... as) : value(std::forward<As>(as)...)
{ std::cout << "emplace constructor" << std::endl; }
wrapper (wrapper const & w0) : value{w0.value}
{ std::cout << "copy constructor" << std::endl; }
wrapper (wrapper && …Run Code Online (Sandbox Code Playgroud) c++ templates constructor-overloading variadic-templates c++11
这个问题很可能已经被问到了,但是我没有找到答案。
下面的代码使用gcc进行编译,但在运行时因std :: length_error(live)崩溃。
void test(const std::string &value) { std::cout << "string overload: " << value << std::endl; }
//void test(const std::vector<std::string> &) { std::cout << "vector overload" << std::endl; }
int main()
{
test({"one", "two"});
}
Run Code Online (Sandbox Code Playgroud)
从字符串的初始化程序列表创建字符串的功能似乎有争议,例如,不能使上面的代码注释掉的重载成为可能。
但是,即使允许这种构造,为什么还会导致失败呢?
c++ constructor constructor-overloading initializer-list overload-resolution
#include <iostream>
#include <string>
struct mystruct{
mystruct(std::string s){
std::cout<<__FUNCTION__ <<" String "<<s;
}
explicit mystruct(bool s) {
std::cout<<__FUNCTION__<<" Bool "<<s;
}
};
int main()
{
const char* c ="hello";
mystruct obj(c);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
mystruct Bool 1
Run Code Online (Sandbox Code Playgroud)
const char*隐式转换为bool而不是std::string,虽然构造函数需要explicit类型?c++ explicit constructor-overloading overload-resolution implicit-conversion
警告:我一直在学习Python 10分钟,所以对任何愚蠢的问题表示歉意!
我写了以下代码,但是我得到以下异常:
消息文件名行位置跟踪节点31 exceptions.TypeError:此构造函数不带参数
class Computer:
name = "Computer1"
ip = "0.0.0.0"
screenSize = 17
def Computer(compName, compIp, compScreenSize):
name = compName
ip = compIp
screenSize = compScreenSize
printStats()
return
def Computer():
printStats()
return
def printStats():
print "Computer Statistics: --------------------------------"
print "Name:" + name
print "IP:" + ip
print "ScreenSize:" , screenSize // cannot concatenate 'str' and 'tuple' objects
print "-----------------------------------------------------"
return
comp1 = Computer()
comp2 = Computer("The best computer in the world", "27.1.0.128",22)
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?
我有几种类型来自简化Base,如下所示.
我不确定this在重载构造函数时是否使用基类的构造函数或构造函数.
ConcreteA重载构造函数纯粹使用base构造函数,而
ConcreteB重载this用于前两个重载.
什么是重载构造函数的更好方法?
public abstract class Base
{
public string Name { get; set; }
public int? Age { get; set; }
protected Base() : this(string.Empty) {}
protected Base(string name) : this(name, null) {}
protected Base(string name, int? age)
{
Name = name;
Age = age;
}
}
public class ConcreteA : Base
{
public ConcreteA(){}
public ConcreteA(string name) : base(name) {}
public ConcreteA(string name, int? age) : …Run Code Online (Sandbox Code Playgroud) 假设我有这个虚拟类定义:
class Node
{
public:
Node ();
Node (const int = 0);
int getVal();
private:
int val;
};
Run Code Online (Sandbox Code Playgroud)
虚拟构造函数实现仅用于教育目的:
Node::Node () : val(-1)
{
cout << "Node:: DEFAULT CONSTRUCTOR" << endl;
}
Node::Node(const int v) : val(v)
{
cout << "Node:: CONV CONSTRUCTOR val=" << v << endl;
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我编译(使用选项-Wall -Weffc++ -std=c++11:),下面的代码:
#include <iostream>
#include "node.h"
using namespace std;
int main()
{
Node n;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误,根本不编译:
node_client.CPP: In function ‘int main()’:
node_client.CPP:10:16: error: call of …Run Code Online (Sandbox Code Playgroud) 我正在尝试构建一个构造函数,将一个数组作为一个参数,重载另一个采用标量的人.代码如下.
#include <iostream>
template <typename T>
class SmallVec { // This is a 3 dimensional vector class template
public:
T data[3] = {0}; // internal data of class
template <typename U>
explicit SmallVec(const U& scalar) { // if a scalar, copy it to each element in data
for(auto &item : data) {
item = static_cast<T>(scalar);
}
}
template <typename U>
explicit SmallVec(const U* vec) { // if a vector, copy one by one
for(auto &item : data) {
item = …Run Code Online (Sandbox Code Playgroud) c++ ×7
constructor ×5
c++11 ×3
templates ×3
overloading ×2
.net ×1
ambiguous ×1
arrays ×1
c# ×1
default ×1
ecmascript-6 ×1
exception ×1
explicit ×1
javascript ×1
python ×1