当我将enum指定为第二个参数时,为什么VisualC++(2008)会混淆"C2666:2重载具有类似的转换",但是当我定义bool类型时却没有?
不应该键入匹配已排除第二个构造函数,因为它是'basic_string'类型?
#include <string>
using namespace std;
enum EMyEnum { mbOne, mbTwo };
class test {
public:
#if 1 // 0 = COMPILE_OK, 1 = COMPILE_FAIL
test(basic_string<char> myString, EMyEnum myBool2) { }
test(bool myBool, bool myBool2) { }
#else
test(basic_string<char> myString, bool myBool2) { }
test(bool myBool, bool myBool2) { }
#endif
};
void testme() {
test("test", mbOne);
}
Run Code Online (Sandbox Code Playgroud)
我可以通过指定引用来解决这个问题.basic_string&myString'但不是'const basic_string&myString'.
也通过"test((basic_string)"test",mbOne"显式调用;" 也有效.
我怀疑这与通过固有的'!= 0'解析为bool的每个表达式/类型有关.
好奇的评论都一样:)
为什么以下尝试重载构造函数Foo::Foo失败?此外,我很欣赏替代方案/解决方法
#include <vector>
#include <type_traits>
namespace xyz
{
struct MemoryManager{};
template<typename T , typename Alloc=MemoryManager>
class vector
{
};
}
template<typename T, template<typename,typename> class V , typename A>
struct Foo
{
template<typename U = T ,
typename Dummy = typename std::enable_if<
std::is_same<std::vector<U>, V<U,A> >::value >::type >
Foo() // when instantiated with the std vector
{
}
template<typename U = T ,
typename Dummy = typename std::enable_if<
std::is_same<xyz::vector<U>, V<U,A> >::value >::type >
Foo() // when instantiated with …Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的问题但到目前为止找不到任何东西.
我正在尝试创建两个类构造函数.
第一个构造函数获取2个字符串和一个HashMap并初始化类变量.
public Foo(String a, String b, HashMap<String, String> c) {
this.a = a;
this.b = b;
this.c = c;
}
Run Code Online (Sandbox Code Playgroud)
第二个构造函数应该只获取2个字符串并创建一个"默认"-HashMap.
通常你只需要this()使用默认值调用,但我找不到用a做的方法HashMap.
public Foo(String a, String b) {
this(a, b, new HashMap<String, String>().put("x", "y").put("f","g"));
}
Run Code Online (Sandbox Code Playgroud)
Eclipse标记错误:
类型不匹配:无法转换
String为HashMap<String,String>
否则this()-call不能是函数中的第一个语句.
public Foo(String a, String b) {
HashMap<String, String> c = new HashMap<String, String>();
c.put("x", "y");
c.put("f", "g");
this(a, b, c);
}
Run Code Online (Sandbox Code Playgroud)
任何想法如何解决这个问题?
最糟糕的情况我不得不复制代码,但我想知道是否没有更好的方法.
当使用来自二级子类的超类构造函数时,它是否将参数传递给祖父项构造函数或直接父构造函数?
//top class
public First(type first){
varFirst = first;
}
//child of First
public Second(type second){
super(second); //calls First(second)
}
//child of Second
public Third(type third){
super(third); //calls First(third) or Second(third)?
}
Run Code Online (Sandbox Code Playgroud) 我有两个构造函数,它们的lambda返回类型不同。有什么选择可以使它们超载吗?我试图使用JvmOverloads注释,但是没有用。
constructor(db : Database, handler: ( transaction: Transaction) -> Unit) : this(db, Handler<Transaction>( {handler.invoke(it)}))
@JvmOverloads
constructor(db : Database, handler: ( transaction: Transaction) -> Any) : this(db, Handler<Transaction>( {handler.invoke(it)}))
Run Code Online (Sandbox Code Playgroud) 美好的一天.
我正在研究处理任务的事情.
每个任务由3个字符串和一个DateTime对象组成
下面是我构建的构造函数.
public Task(string von, string was, string an, DateTime zeit = DateTime.Now)
Run Code Online (Sandbox Code Playgroud)
编译时我得到编译器错误
Default parameter value for 'zeit' must be a compile-time constant (CS1736)
我假设问题是,-obvioulsy- DateTime.Now的值取决于构造函数被调用的时间,这是我想要的全部内容.
我已经看过[this]线程,但它并不真正适用于我,因为DateTime对象的内存需求总是相同的,并且该线程说问题是该调用的未知堆需求.1
我已经有了一个解决方法的想法(见下文),但我们都知道解决方法不是最佳实践
public Task(string von, string was, string an, DateTime zeit){
if(zeit == null)
dateOfCreation = DateTime.Now; //dateOfCreation being the name of Task's internal DateTime field.
else
dateOfCretion = zeit;
Run Code Online (Sandbox Code Playgroud)
所以,如果我想使用当前的DateTime,我会传递null.但是:如果我花时间和精力始终专门传递null,我不妨每次都传递DateTime.Now.
问题:有没有办法让参数DateTime zeit = DateTime.Now被接受或替换为相同的结果?
我试图将构造函数重载到一个类,以便它可以接受两种不同类型的对象的列表:
class myClass(){
var someStrings: List[String]=List[String]()
println("hello!")
def this(strings : List[String])={
this()
this.someStrings=strings
}
def this(ints: List[Int])={
this()
this.someStrings=ints.map(x => x.toString)
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,接受一个int或字符串列表,并将字符串列表保存到变量someStrings.上面的代码不起作用:
error: double definition:
constructor myClass: (strings: List[String])myClass at line 12 and
constructor myClass: (ints: List[Int])myClass at line 17
have same type after erasure: (strings: List)myClass
def this(ints: List[Int])={
^
Run Code Online (Sandbox Code Playgroud)
在scala中有更好的方法吗?(除了列出[任何]并测试元素)
谢谢!
我有一个非空终止的字符向量,如何构造一个新字符串并让它自动插入\0到字符串的末尾?
std::vector<char> v;
v.push_back('H');
v.push_back('i');
v.push_back('!');
//v.push_back('\0'); <~ without using this line
std::string a(v.data());
std::string b(v.begin(), v.end()); // same meaning as b(v.data(), v.size())
Run Code Online (Sandbox Code Playgroud)
两者之间的正确构造函数是什么?
我正在学习 C# 并制作了一个简单的“播放器”类。但我很难有多重超载。这是我最好的解决方案,但我觉得它可以做得更简单/更好。
class Player : Entity
{
public Player() {
Name = "Player";
XP = 0;
LVL = 1;
XPToLvlUp = 10;
XpRank = 10;
}
public Player(string name) : this() {
Name = name;
}
public Player(string name, int _Hp, int _Mp) : this(name) {
HP = _Hp;
MP = _Mp;
}
public Player(string name, int _Hp, int _Mp, int _Xp, int _Lvl) : this(name, _Hp, _Mp) {
XP = _Xp;
LVL = _Lvl;
}
public Player(string name, …Run Code Online (Sandbox Code Playgroud) 来自 JavaScript 和 Python,我试图了解 C++ 类构造函数的细微差别和目的。
在下面的示例中,为什么允许在没有构造函数的情况下初始化属性?
class MyClass {
public:
int a = 1;
int b = 2;
};
Run Code Online (Sandbox Code Playgroud)
默认构造函数是否包括上述定义/初始化?下面两个例子有什么区别?:
1.
class MyClass {
public:
int a;
int b;
MyClass(){
a = 1;
b = 2;
}
};
Run Code Online (Sandbox Code Playgroud)
class MyClass {
public:
MyClass(){
// Some kind of variable declaration and definition like:
// this.a = 1;
// this.b = 2;
}
};
Run Code Online (Sandbox Code Playgroud)
对我来说,在没有构造函数的情况下进行初始化的选项听起来有点矫枉过正,而且令人困惑。在 Python 和 JavaScript 中,通常从构造函数声明和初始化所有变量,并且仅从构造函数开始。
这里的最佳做法是什么?
c++ constructor class constructor-overloading default-constructor