标签: constructor-overloading

可选参考成员 - 是否可能?

我有以下课程

class CItem
{
    public:
        CItem(CRegistry &Registry) _Registry(Registry) {Registry.Register();}
        ~CItem() {_Registry.Unregister()};


    private:
        CRegistry &_Registry;
}
Run Code Online (Sandbox Code Playgroud)

经过一段时间后,事实证明并非所有CItem对象都需要注册,因此我需要一个CItem版本,它不需要构造函数中的Registry(当然还有注册代码).我该如何实现呢?我在这里看到的唯一解决方案是获取并保持Registry作为指针.是否有更优雅的解决方案,如使用模板等(我不喜欢从引用切换到指针)?

c++ class constructor-overloading

2
推荐指数
2
解决办法
1869
查看次数

是否可以为std :: array类型添加自己的构造函数?

我试图为std::array类型添加我自己的构造函数,但我不确定是否可能以及如何做到这一点......

我试过这样的事:

typedef unsigned char byte_t;

namespace std {
  template<std::size_t _Nm>
  array::array(std::vector<byte_t> data)
  {
    // Some content
  }
}
Run Code Online (Sandbox Code Playgroud)

我想创建一个非常简单的机制来转换std::vector<byte_t>std::array<byte_t, size>.

  1. 有可能的?
  2. 我怎样才能做到这一点?

我正在使用C++ 14(我不能在我的项目中使用更新的标准)

c++ constructor-overloading stdvector stdarray c++14

2
推荐指数
1
解决办法
127
查看次数

为什么派生类中复制/移动函数的"正常"实现的行为会有所不同,具体取决于它们的定义方式?

当派生类复制和移动函数调用它们的基类版本时,我对我所看到的行为感到困惑.

我有一个基类与各种构造函数,告诉我什么时候被调用:

#include <iostream>

class Base {
public:     
  Base() {}

  template<typename T>
  Base(T&&) { std::cout << "URef ctor\n"; }

  Base(const Base&) { std::cout << "Copy ctor\n"; }

  Base(Base& rhs): Base(const_cast<const Base&>(rhs))
  { std::cout << "  (from non-const copy ctor)\n"; }

  Base(Base&&) { std::cout << "Move ctor\n"; }

  Base(const Base&& rhs): Base(rhs)
  { std::cout << "  (from const move ctor)\n"; }
};
Run Code Online (Sandbox Code Playgroud)

对于具有编译器生成的复制和移动操作的派生类

class Derived: public Base {};
Run Code Online (Sandbox Code Playgroud)

和这个测试代码,

int main()                              
{                                      
  Derived d;                         
  Derived copyNCLValue(d);          
  Derived copyNCRvalue(std::move(d)); 

  const Derived cd; …
Run Code Online (Sandbox Code Playgroud)

c++ constructor-overloading c++11 universal-reference

1
推荐指数
1
解决办法
124
查看次数

用Java链接构造函数

无法提出更好的标题。

一个经典的学习示例:class Human,其中属性是姓名,年龄,母亲和父亲。父母双方Human也是。

public class Human {
    String name;
    int age;
    Human mother;
}
Run Code Online (Sandbox Code Playgroud)

我想创建3个构造函数:

  1. Human();
  2. Human(String name, int age);
  3. Human(String name, int age, Human mother)

我确实了解链接的工作原理,这就是我所做的:

Human() {
    this("Jack", 22);
}

Human(int age, String name) {
    this(age, name, new Human()); // new Human() will cause SOF Error.
}

Human(int age, String name, Human mother) {
    this.age = age;
    this.name = name;
    this.mother = mother;
}
Run Code Online (Sandbox Code Playgroud)

如上所述,我StackOverflowError再次收到,我我知道为什么会这样。公平地说,我想我会得到像人类杰克这样的东西,而他的母亲也是人类杰克 …

java constructor-overloading constructor-chaining

1
推荐指数
1
解决办法
64
查看次数

c++ 是否可以在不基于其基类的派生类中创建构造函数?

执行下面的代码将给出以下行的错误Child(){/**........code*/}:没有匹配的函数用于调用 'Base::Base()' 并且候选 Base::Base(int) 需要 1 个参数,并且没有提供。

class Base
{

public:
    Base(int test)
    {

    };

};

class Child : public Base
{

public:
    Child ()
    {
        /**....
           ....
           code*/
    };
    Child(int test):Base(test)
    {

    };
};
Run Code Online (Sandbox Code Playgroud)

所以我想知道是否有办法在派生类中使用构造函数 - 例如:Child(){};- 与其基类没有任何关联。

c++ oop inheritance constructor-overloading

1
推荐指数
1
解决办法
60
查看次数

如果调用函数的特定重载,如何引发编译时错误?

根据https://en.cppreference.com/w/cpp/string/basic_string_view/basic_string_viewstd::basic_string_view类有 7 个重载的 ctor。我只关心其中的两个,因为现在我不在我的代码中使用其余的。

这些是我关心的实例:

constexpr basic_string_view( const CharT* s, size_type count );
constexpr basic_string_view( const CharT* s );
Run Code Online (Sandbox Code Playgroud)

我需要防止代码调用第二个代码(因为它可能导致非空终止缓冲区的缓冲区溢出)。

我有类似下面的内容:

#include <iostream>
#include <sstream>
#include <string>
#include <array>

void func( const std::string_view str )
{
    if ( str.empty( ) )
    {
        return;
    }

    std::stringstream ss;

    if ( str.back( ) == '\0' )
    {
        ss << str.data( );
    }
    else
    {
        ss << std::string{ str };
    }
}

int main()
{
    std::array<char, 20> buffer { }; …
Run Code Online (Sandbox Code Playgroud)

c++ constructor-overloading string-view

1
推荐指数
1
解决办法
63
查看次数

根据传递给构造函数的参数数量进行推导

这是我正在尝试的东西,但似乎不起作用:我想根据类对象的实例化方式来切换编译时开关。如果只有一个构造函数参数,则LengthOpt应该等于false,否则等于true(我的实现有多个构造函数,其中开关应默认为true.

我尝试创建一个推导指南,但显然如果模板参数没有显示为构造函数参数(这真是令人沮丧),它就不起作用。有谁知道这个问题的不太详细的解决方案?

代码

#include <cstring>
#include <iostream>

const char* str = "some input string";


template <bool LengthOpt>
class some_class
{
public:
    some_class(const char*) {
        std::cout << std::boolalpha << LengthOpt << std::endl;
    }
    some_class(const char*, size_t len) {
        std::cout << std::boolalpha << LengthOpt << std::endl;
    }
};

template <bool LengthOpt> some_class(const char*) -> some_class<false>;
template <bool LengthOpt> some_class(const char*, size_t) -> some_class<true>;

int main()
{
    some_class A(str);
    some_class B(str, strlen(str));
}
Run Code Online (Sandbox Code Playgroud)

错误 …

c++ constructor-overloading c++17 ctad deduction-guide

1
推荐指数
1
解决办法
124
查看次数

如何让编译器区分 f(double...) 和 f(int, double...)

我正在编写一个用于处理多项式的小型库。

\n

主类称为poly,它包含 4 个重载的构造函数。

\n

类的对象poly是一个多项式的表示。

\n

完整代码可以在这里查看: https: //github.com/IQ8QI/polynomial-lib

\n

./test-poly/test-all.cpp我试图创建一个类的对象poly

\n
poly::poly my_poly = poly::poly((double)6.0, -2.0, 4.0);\n
Run Code Online (Sandbox Code Playgroud)\n

类的构造函数poly有:

\n
//Create polynomial using just numbers\npoly(double values...);\n\n//Create polynomial using vector<double>\npoly(vector<double> values);\n\n//Create polynomial with non-zero base\npoly(int base, double values...);\n\n//Create polynomial with non-zero base and vector\npoly(int base, vector<double> values);\n
Run Code Online (Sandbox Code Playgroud)\n

不幸的是,我收到一个编译错误:

\n
./test-all.cpp:20:63: error: call of overloaded \xe2\x80\x98poly(double, double, double)\xe2\x80\x99 is ambiguous\n   20 |         poly::poly my_poly = poly::poly((double)6.0, -2.0, 4.0);\n …
Run Code Online (Sandbox Code Playgroud)

c++ variadic-functions constructor-overloading

1
推荐指数
1
解决办法
147
查看次数