我是c ++的新手,我使用Qt在VS2010中有以下代码:
// test.h
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_test.h"
#include "testDevice.h"
#include "testControl.h"
class test : public QMainWindow
{
Q_OBJECT
public:
test(QWidget *parent = 0) : control(device,0) {}
~test();
private:
Ui::testClass ui;
testDevice device;
testControl control;
};
// test.cpp
#include "test.h"
test::test(QWidget *parent) : QMainWindow(parent)
{
ui.setupUi(this);
device.start();
control.start();
}
test::~test()
{
}
// testControl.h
#pragma once
#include <QThread>
#include "testDevice.h"
class testControl : public QThread
{
Q_OBJECT
public:
testControl(testDevice& device, QObject *parent = 0);
protected:
void run(void);
private: …Run Code Online (Sandbox Code Playgroud) 函数get1()和get2()的用法有什么不同?
struct x
{
float get1() const
{
float fx = 4;
fx += 1.5f;
return fx;
}
float& get2() const
{
float fx = 4;
fx += 1.5f;
return fx;
}
};
int main()
{
x t;
float x1 = t.get1();
float/*&*/ x2 = t.get2();
cout << x1 << endl;
cout << x2 << endl;
cin.get();
}
Run Code Online (Sandbox Code Playgroud)
据我所知,get2()只能是const类成员..我不完全清楚.如果有人可以指出我的参考或只是一个简短而完整的解决方案,那就太好了.
Node *&front在C++中下面的链表代码提取是什么意思?
FrontBackSpilit(head,a,b)
Node * FrontBackSpilit(Node * head, Node *&front, Node * &back)
Run Code Online (Sandbox Code Playgroud) 我一直在努力学习如何使用单例设计模式并偶然发现:
static S& getInstance()
{
static S instance;
return instance;
}
Run Code Online (Sandbox Code Playgroud)
我注意到它返回了instance,它不应该归还instance&吗?
编辑:我还应该提到编译器似乎没有提出任何投诉.
我有这节课singleton.h:
class SingletonSample {
public:
static SingletonSample& Instance();
int DoSomething() const;
private:
SingletonSample();
~SingletonSample();
SingletonSample(const SingletonSample&) = delete;
SingletonSample& operator=(const SingletonSample&) = delete;
static SingletonSample* _instance;
}
Run Code Online (Sandbox Code Playgroud)
我有这个实现singleton.cpp:
SingletonSample::SingletonSample() {}
SingletonSample::~SingletonSample() {}
SingletonSample& SingletonSample::Instance() {
if(!_instance) _instance = new SingletonSample();
return _instance;
}
int SingletonSample::DoSomething() const {
return 20;
}
Run Code Online (Sandbox Code Playgroud)
尝试时出现编译错误return _instance.
我想要返回一个SingletonSample&,但我_instance是一个指针SingletonSample*.
我是C++的初学者,但据我所知,对某事的引用只是该事物的地址,而指向某事物的指针只是指向该地址的指针.
我想返回指针指向的地址作为参考SingletonSample&.
但是,我无法实现这一目标.我怎样才能做到这一点?
据我所知,可以在原始类型可以使用的地方使用引用(我并不暗示反向是真的),唯一的区别是它们的变异语义(当变量用作左值时).
那么他们不会像原版那样符合资格吗?如果是这样,为什么某些东西是一个参考,存储在它的类型中?
编辑:如果引用是不同的类型,为什么它们可以在很多情况下替换原始类型,而无需显式转换?是否涉及隐式演员?
例:
void bar(int& a);
int x;
int& y = x;
bar(y) // matching type
bar(x) // what happened here? was x cast to a reference?
Run Code Online (Sandbox Code Playgroud) 我知道我不能写long& const x.但为什么不呢?从理论上讲,我可以对x进行持续的引用.我只是无法将引用设置为另一个地址.
为什么在C++中禁止它?
编辑:我知道有些东西我不明白,但我可以指定另一个变量的引用.这段代码有效:
#include <iostream>
int main()
{
int a = 1, b = 2;
int & x = a;
x = b;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但这与你所说的相矛盾.
好的,我现在已经处理了这两天了,我找不到解决办法.
问题:我正在尝试使用Winapi 将过滤器设置为文件选择对话框.我正在使用GetOpenFileName函数来执行此操作.此函数使用结构来设置文件扩展名筛选器等选项.这个结构的成员调用lpstrFilter需要一定的字符串格式.我正在设置与Winapi指示完全相同的字符串,但由于某种原因,此字符串的值会发生变化.
我有这个静态const char*:
//This contains string "JPG"
static const char * extensionFilter = v->trabajo10.C_JMV_SelectFile_FileExtension7.GetString();
//This forms a filter string which applies to OPENFILENAME structure.
string sFilter;
sFilter.append("Format: ");
sFilter.append(extensionFilter);
sFilter.push_back('\0');
sFilter.append("*.");
sFilter.append(extensionFilter);
sFilter.push_back('\0');
const char * filter = sFilter.c_str();
ofn.lpstrFilter = filter; //This sets: --> Format: JPG\0*.JPG\0
//This opens the file selection dialog
if (GetOpenFileName(&ofn)==TRUE){
...
Run Code Online (Sandbox Code Playgroud)
文件选择对话框看起来正确,如下所示:
这个笑话现在来了,我修改了这样的代码:
//This contains string "JPG"
static const char …Run Code Online (Sandbox Code Playgroud) 我试图id在C++中使用以下函数.我原以为我可以干脆做;
typedef int v3[3];
v3 & id( v3 & in )
{
in[0] = in[1] = in[2] = 1;
return in;
}
int main()
{
v3 & v = id( v );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
然而,这导致了一个段错g++(我不清楚).因为我可以使用标量类型执行以下操作:
int & zero( int & in )
{
in = 0;
return in;
}
int main()
{
int i = zero( i );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何使用id单行代码?我想避免一个丑陋的解决方案,例如:
int main()
{
v3 v;
v3 & w = id( v …Run Code Online (Sandbox Code Playgroud) 我创建类库,创建Web应用程序项目,然后添加引用类库的Web应用程序项目按此处的说明,而是"利用"它不能添加.(customer.cs的命名空间也是"CDemoLib")(当我添加"使用CDemoLib"时,我得到'无法解析符号CDemoLib'
customer.cs的代码是
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CDemoLib
{
public class Customer
{
private int _age;
private string _name;
public Customer()
{
Age = 0;
Name = "Not available";
}
public int Age
{
get { return _age; }
set { _age = value; }
}
public string Name
{
get { return _name; }
set { _name = value; }
}
}
}
Run Code Online (Sandbox Code Playgroud)