标签: initializer

C++:newbie初始化列表问题

新手在这里.我在看公司代码.

似乎A类中没有成员变量但是在A的构造函数中它初始化了一个对象B,即使类A不包含任何类型B的成员变量(或任何成员变量!).

我想我甚至不能理解问题......所以这里发生了什么!?我的直觉是你甚至在尝试初始化它之前需要一个变量.如何在没有对象的情况下初始化对象有可能(或者有什么用呢)?

.H:

class A: public B
{
public:
     A(bool r = true);
     virtual ~A;

private:
}
Run Code Online (Sandbox Code Playgroud)

的.cpp:

A::A(bool r) : B(r ? B::someEnumeration : B::anotherEnumeration)
{
}

A::~A()
{
}
Run Code Online (Sandbox Code Playgroud)

请帮忙.

谢谢,jbu

c++ list initializer

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

将Lua方法定义为初始化

在Lua语言中,我能够在表格中定义诸如的函数

table = { myfunction = function(x) return x end }
Run Code Online (Sandbox Code Playgroud)

我想知道我是否可以用这种方式创建方法,而不是像我这样做

function table:mymethod() ... end
Run Code Online (Sandbox Code Playgroud)

我很确定可以用这种方式添加方法,但我不确定这种技术的正确名称,我找不到它寻找"lua"和"方法"等.

我的目的是将表传递给诸如的函数myfunction({data= stuff, name = returnedName, ?method?init() = stuff}).

不幸的是,我尝试了几种冒号方法声明的组合,但它们都不是有效的语法.

那么......这里有人碰巧知道吗?

methods lua initializer

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

Char数组初始化困境

考虑以下代码:

// hacky, since "123" is 4 chars long (including terminating 0)
char symbols[3] = "123";

// clean, but lot of typing
char symbols[3] = {'1', '2', '3'};
Run Code Online (Sandbox Code Playgroud)

所以,扭曲实际上是在对代码的注释中描述的,有没有办法char[]用字符串文字初始化而不终止零?

更新:看起来IntelliSense确实是错误的,这种行为在C标准中明确定义.

c arrays initialization initializer

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

C++数组初始化器.使用枚举类型

class ARouter {
    enum directions {north, neast, east, seast, south, swest, west, nwest};
    static directions gon[] = {north, neast, nwest, east, west, seast, swest, south};
};
Run Code Online (Sandbox Code Playgroud)

嗨,有谁知道上面的代码是什么问题?

我从VC++ 2008Ex获得第二行的2个错误:

错误C2059:语法错误:'{'

错误C2334:'{'之前的意外标记; 跳过明显的功能体

c++ arrays enums initializer

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

Java初始化块

有人能帮助我理解以下构造吗?我无法理解这是初始化程序还是匿名类.我不熟悉这种语法.

   JTable jt = new JTable(data, fields) **{
            public TableCellRenderer getCellRenderer(int row, int column) {
                // TODO Auto-generated method stub
                return renderer;
            }
        };**
Run Code Online (Sandbox Code Playgroud)

java swing jtable initializer anonymous-class

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

模板<class T>错误:'模板'之前的预期初始化程序

嗨stackoverflow论坛的人,我直接从教科书中输入了这段代码,Absolute C++ Fourth Edition Savitch ISBN-13:978-0-13-136584-1.通用排序功能.第728页的sort.cpp给出了第17行的错误:第17行:错误:'template'之前的预期初始化程序

有人可以提供帮助,因为我希望教科书"正常工作",这样我就可以研究代码而不会陷入我不理解的额外错误.是的,我已经研究过,但是这个错误的研究是有限的,因为我专注于通用排序功能的简单学习点,希望学习通用模板,希望学习哈希表... phewww,喘口气.

我无法对出现错误的第17行进行粗体显示.

// This is the file sort.cpp.
template<class T>
void sort(T a[], int numberUsed)
{
    int indexOfNextSmallest;
    for (int index = 0; index < numberUsed - 1; index++)
    {//Place the correct value in a[index]:
        indexOfNextSmallest =
            indexOfSmallest(a, index, numberUsed);
        swapValues(a[index], a[indexOfNextSmallest]);
    //a[0] <= a[1] <=...<= a[index] are the smallest of the original array
    //elements. The rest of the elements are in the remaining positions.
    }
}
template<class T>
void swapValues(T& variable1, T& …
Run Code Online (Sandbox Code Playgroud)

c++ initializer

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

关于c#中的集合初始值设定项

我承认我对c#的经验很少,所以这可能很明显,但我不得不问 - 两个代码示例之间有什么区别吗?如果不明显,第一个语句会在new运算符的末尾省略().那里有什么区别或者()在这种情况下是多余的?

private static Dictionary<string, string> dict1 = new Dictionary<string, string>
{
    { "a", "A" },
    { "b", "B" }
};

private static Dictionary<string, string> dict2 = new Dictionary<string, string>()
{
    { "a", "A" },
    { "b", "B" }
};
Run Code Online (Sandbox Code Playgroud)

c# collections initializer

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

在Swift中调用只具有类名的初始化程序

我想知道是否有办法只通过在Swift中拥有类名来调用初始化程序.

class Shape {
  let name: String
  var numberOfSides: Int?

  init(name: String) {
    self.name = name
  }
}
Run Code Online (Sandbox Code Playgroud)

伪代码:

let shapeClass = stringToClass("Shape")

let shape: Shape = shapeClass(name: "Something")
Run Code Online (Sandbox Code Playgroud)

initializer ios swift

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

C#Constructor重载:new object.FromOtherObject()?

我有两种不同类型的节点,我需要能够以相对简单的方式在它们之间进行转换.我正在考虑在构造函数中执行此操作,因为它会使代码更清晰.

NodeA nodea = new NodeA();

NodeB nodeb = new NodeB.FromNodeA(nodea);
Run Code Online (Sandbox Code Playgroud)

我一直在谷歌搜索几个小时,但未能找到办法做到这一点.我提出的最佳解决方案是;

public NodeB() { }

public static NodeB FromNodeA(NodeA theNodeA)
{
    NodeB b = new NodeB();
    b.Value = theNodeA.value;
    // etc..
}
Run Code Online (Sandbox Code Playgroud)

但是这使代码看起来更像一个时髦的东西;

NodeB b = NodeB.FromNodeA(nodea);
Run Code Online (Sandbox Code Playgroud)

理想情况下,我希望能够这样做:

NodeB b = new NodeB().FromNodeA(nodea);
Run Code Online (Sandbox Code Playgroud)

编辑; 或者是静态方法我已经做了更多(传统上)正确的方法来完成这个?

c# constructor overloading initialization initializer

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

不能多次初始化var变量

嗨我有一个var变量,我需要根据if语句初始化它这是我的代码:

var series = new ColumnSeries{};
if(integer == 0)
  series = new LineSeries{};
else if (integer == 1)
  series = new PieSeries{};
else if (integer == 2)
  series = new AreaSeries{};
Run Code Online (Sandbox Code Playgroud)

但它得到错误,我不能多次初始化变量,所以我怎么能不止一次初始化这个变量?我需要根据if语句更改我的图表类型,所以我尝试了这种方式.

c# var initializer livecharts

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