如果我想要一个循环引用,但在C++中的两个不同的文件中,我将如何实现它?
例如
AUnit.h
#inclue <BUnit.h>
class AClass : public TObject
{
__published
BClass * B;
};
Run Code Online (Sandbox Code Playgroud)
BUnit.h
#include <AUnit.h>
class BClass : public TObject
{
__published
AClass *A;
};
Run Code Online (Sandbox Code Playgroud)
我只能在一个带有前向声明的文件中创建它.
是否可以在 Objective C 头文件中对命名空间内的 C++ 类进行前向声明?
在目标 C 中转发声明的 C++ 类:
namespace name
{
class Clazz
{
};
}
Run Code Online (Sandbox Code Playgroud)
我知道如果 C++ 类不在命名空间中,我可以。取自http://www.zedkep.com/blog/index.php?/archives/247-Forward-declaring-C++-classes-in-Objective-C.html
有什么想法吗?
谢谢你们!
有人可以向我解释为什么以下编译:
int main()
{
int a = mymethod(0);
}
int mymethod(int b)
{
return b;
}
Run Code Online (Sandbox Code Playgroud)
但这不会:
int main()
{
mymethod(0);
}
void mymethod(int b)
{
return;
}
Run Code Online (Sandbox Code Playgroud)
我认为在 C/C++ 中需要前向声明,但这里有一个反例。隐式声明如何在 C 中工作?
这里我有state_machine.h:
#ifndef STATE_MACHINE_H
#define STATE_MACHINE_H
// state machine classes
//#include "state_t.h"
class state_t;
class state_machine
{
public:
state_machine();
void change_state(state_t *newState);
void process_state(int data);
private:
state_t *_state;
};
#endif // STATE_MACHINE_H
Run Code Online (Sandbox Code Playgroud)
这是state_t.h:
#ifndef STATE_T_H
#define STATE_T_H
#include <QByteArray>
#include <QDebug>
//#include "state_machine.h"
class state_machine;
class state_t
{
public:
state_t(QByteArray stateName);
virtual ~state_t();
virtual void processState(state_machine *sm, int input) = 0;
void defaultUnknownEventHandler(int event);
QByteArray name;
};
#endif // STATE_T_H
Run Code Online (Sandbox Code Playgroud)
然后是一些状态类,它们或多或少都是相同的,我只列出一个:
测试状态1.h:
#ifndef TESTSTATE1_H
#define TESTSTATE1_H
#include "state_t.h"
class testState1 : …Run Code Online (Sandbox Code Playgroud) 我用 C 编写了一个小代码,其中定义了两个结构类型,它们在定义中具有彼此的成员。情况 1:如果 struct foo 在 struct bar 之前定义,则代码按预期编译。情况 2:如果 struct foo 在 struct bar 之后定义,它将不会编译,这也是预期的,因为无法知道 struct foo 变量的内存要求。但是我期待如果在情况 2 中使用 struct foo 的前向声明它会编译。但它不起作用。我错过了什么?
#include<stdio.h>
#include<stdlib.h>
struct foo; // forward declaration
struct bar
{
int a;
struct bar *next;
struct foo ch;
};
struct foo
{
struct bar *n;
struct foo *nx;
};
int main()
{
struct bar p;
return(0);
}
Run Code Online (Sandbox Code Playgroud) 问题:用smart
替换裸指针,并将它们与前向声明的类一起使用。 QPointers
背景:
正如几乎所有关于现代 C++ 的文本所建议的那样,应该避免使用裸指针,而应使用智能指针。在 Qt 工具包中,有一组智能指针可用,在这种情况下我特别感兴趣:QWeakPointer(Qt 4) 或QPointer(Qt 5),据说可以与裸指针自由互换,并NULL在各自的指针时设置为对象(派生自QObject)被删除,从而有助于防止太常见的悬空指针问题。然而,QPointerbe指向的对象是QObject派生类型的要求阻止了前向声明类的使用。
问题:如何将QPointer-type 变量与类头中的前向声明类与前向声明类型结合起来?
或者
如何避免包含我想要使用的对象的整个头文件QPointer?
例子:
#pragma once
#include <QObject>
#include <QPointer>
class MyQWidget; // Forward-declared class
class SomeClass {
QPointer<MyQWidget> m_myWidget;
};
Run Code Online (Sandbox Code Playgroud)
注意:我已经阅读了这样的 wuestion: 在 C++ 中,是否可以将一个类向前声明为从另一个类继承?. 在我的问题中,我正在寻找特定于 Qt 的方法来克服此限制(或更好的设计功能),如果有的话。
编辑:我的主要编译器是 MSVC 2010,我得到的错误是
error C2079: 'MainWindow::m_test' uses …Run Code Online (Sandbox Code Playgroud) 我如何允许两个类相互包含,以便它们可以从一个类转换为另一个类。
汽车.hpp
#ifndef CAR_HPP
#define CAR_HPP
#include "Truck.hpp"
class Car
{
public:
Car(int weight) : weight(weight) {}
Car(Truck data) : weight(ConvertFromTruck(data)) {}
private:
int weight;
int ConvertFromTruck(Truck data)
{
... //in real life there would be a lot more to transfer than just weight.
}
}
#endif //CAR_HPP
Run Code Online (Sandbox Code Playgroud)
卡车.hpp
#ifndef TRUCK_HPP
#define TRUCK_HPP
#include "Car.hpp" //Obviously won't be included because of the CAR_HPP include guard
class Truck
{
public:
Truck(int weight) : weight(weight) {}
Truck(Car data) : weight(ConvertFromCar(data)) {}
private: …Run Code Online (Sandbox Code Playgroud) 我使用以下简单文件重现错误.
它说:
字段有不完整类型'Foo'
bar.h:
class Foo;
class Bar{
private:
int x_;
Foo foo_; // error: incomplete type
public:
void setx(int x) {x_ = x;};
void increment(int);
};
class Foo{
public:
void run(int y,Bar& bar) {bar.setx(y);};
};
void Bar::increment(int i){foo_.run(i,*this);}
Run Code Online (Sandbox Code Playgroud)
成员foo_不能是引用或指针.原因是在我的实际代码中,我无法在Bar的初始化列表中初始化Foo.
谷歌c ++风格有以下几点.我不明白为什么前向声明会调用f(void*).
可能很难确定是否需要前向声明或完整#include.用前向声明替换#include可以默默地改变代码的含义:
// b.h:
struct B {};
struct D : B {};
// good_user.cc:
#include "b.h"
void f(B*);
void f(void*);
void test(D* x) { f(x); } // calls f(B*)
Run Code Online (Sandbox Code Playgroud)
如果#include被B和D的forward decls替换,test()将调用f(void*).
根据我检查的文档,time_t time(time_t *seconds)在<time.h>头文件下声明。然而,当我运行这段代码时:
#include<stdio.h>
#include<stdlib.h>
int main()
{
srand(time(NULL));
printf("%d",rand());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
不包括<time.h>,它工作正常,或者至少工作。怎么会这样?
c header-files forward-declaration function-declaration c-header
c++ ×7
c ×3
header-files ×2
c-header ×1
class ×1
function ×1
include ×1
namespaces ×1
objective-c ×1
qt ×1
qt5 ×1
struct ×1