我有以下两个类:
TcmTPDataPanel = class(TcmTPBasePanel)
Database: TnxDatabase;
Session: TnxSession;
private
FDataConnector: TcmTPDataConnector;
MyNxDataBase: TnxDatabase;
MyNxSession: TnxSession;
MyRefNxDataBase: TnxDatabase;
protected
procedure Disconnect; virtual; abstract;
procedure Refresh; virtual;
procedure Requery; virtual; abstract;
public
procedure Connect;
published
property DataConnector: TcmTPDataConnector read FDataConnector write
FDataConnector;
end;
TcmTPDataConnector = class(TComponent)
private
FDatabase: TnxDatabase;
FObservers: TList;
FTableForCategories: TnxTable;
FTableForItemCategoryLinks: TnxTable;
FTableForItems: TnxTable;
procedure SetTableForItemCategoryLinks(const Value: TnxTable);
protected
procedure IterateObservers;
public
constructor Create;
destructor Destroy; override;
procedure Register(Instance: TcmTPDataPanel);
procedure Unregister(Instance: TcmTPDataPanel);
published
property Database: TnxDatabase read FDatabase …Run Code Online (Sandbox Code Playgroud) 我的班级遇到了一些问题,因为它们彼此依赖,如果没有宣布另一个,就不能宣布.
class block: GtkEventBox {
public:
block(board board,guint x,guint y): image("block.png") {
this.board = board;
this.x = x;
this.y = y;
board.attach(this,x,y,x+1,y+1);
}
void move(guint x,guint y) {
board.remove(this);
this.x = x;
this.y = y;
board.attach(this,x,y,x+1,y+1);
}
private:
guint x, y;
board board;
GtkImage image;
};
class board: Gtk::Table {
public:
board(): Gtk::Table(25,20) {
blocks_c = 0;
}
void addBlock(guint x,guint y) {
blocks_a[blocks_c++] = new block(this,x,y);
}
private:
block* blocks_a[24];
int blocks_c;
};
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,"块"类需要知道"板"是什么,反之亦然.提前致谢!
我正在为游戏引擎制作基于组件的实体系统.
我有一个实体类,它必须包含组件基类头以定义组件数组 private Component* components[ 123 ]
但是,在组件基类中我必须定义一个private Entity* ownerEntity,因为组件知道它属于谁是至关重要的!
这导致Entity.h需要Component.h,反之亦然 - >循环引用
我怎么解决这个问题?
我有三个类:a TopClass包含一个BottomClass指针实例.该BottomClass包含一个指向HelperClass.该HelperClass保持一个指针TopClass.弹出循环依赖关系并且需要前向声明HelperClass.
所有这些都通过以下代码片段进行说明:
#include "BottomLevel.h"
namespace foo
{
class TopLevel
{
private:
BottomLevel* item;
};
}
Run Code Online (Sandbox Code Playgroud)
-
#include "HelperClass.h"
namespace foo
{
class BottomLevel
{
private:
HelperClass* item;
};
}
Run Code Online (Sandbox Code Playgroud)
-
class TopLevel; // forward declaration here
namespace foo
{
class HelperClass
{
public
HelperClass(TopLevel* item);
};
}
Run Code Online (Sandbox Code Playgroud)
尝试在实现文件中执行操作时会出现问题.如果我#include "TopClass.h"在cpp文件中,我会收到编译错误,指出"找不到重载的成员函数 - 使用未定义类型' TopLevel'"(ERRORS C2511和C2027).
然后,如果我不这样做,#include我仍然会留下C2027错误,因为我尝试使用前向声明的类型.
我只知道有一种方法可以解决这个问题,我相信我之前已经做过了,但我不能为我的生活记住我应该做的事情.有什么帮助吗?
我阅读了很多循环依赖主题,但所有这些主题似乎都与声明有关.我感兴趣的是如何构建相互依赖的对象,以及我的方法是否存在潜在的缺陷.考虑这个简单的例子:
#include <iostream>
#include <vector>
using namespace std;
class A; //Forward declaration
class B{
public:
B(string name, A* a):myA(a), name(name){
cout << "Works with pointer" << endl;
};
private:
A* myA;
string name;
};
class A{
public:
A(){
cout << "Constructing A" << endl;
if(bs.empty()) cout << "Vector is empty" << endl;
bs.push_back(B("First", this));
cout << "Array has " << bs.size() << " elements." << endl;
};
private:
std::vector<B> bs;
};
int main() {
cout << "Start" << endl;
A …Run Code Online (Sandbox Code Playgroud) 我认为我有一个循环依赖问题,并且不知道如何解决它....尽可能短:我编写类似html解析器的东西.我有一个main.cpp文件和两个头文件Parser.h和Form.h. 这些头文件包含整个定义......(我懒得制作相应的.cpp文件......
Form.h看起来像这样:
//... standard includes like iostream....
#ifndef Form_h_included
#define Form_h_included
#include "Parser.h"
class Form {
public:
void parse (stringstream& ss) {
// FIXME: the following like throws compilation error: 'Parser' : is not a class or namespace name
properties = Parser::parseTagAttributes(ss);
string tag = Parser::getNextTag(ss);
while (tag != "/form") {
continue;
}
ss.ignore(); // >
}
// ....
};
#endif
Run Code Online (Sandbox Code Playgroud)
和Parser.h看起来像这样:
// STL includes
#ifndef Parser_h_included
#define Parser_h_included
#include "Form.h"
using namespace std;
class Parser {
public:
void …Run Code Online (Sandbox Code Playgroud) 我有以下服务类:
public class JobService {
private UserService us;
public JobService (UserService us) {
this.us = us;
}
public void addJob(Job job) {
// needs to make a call to user service to update some user info
// similar dependency to the deleteUser method
}
}
public class UserService {
private JobService js;
public UserService(JobService js) {
this.js = js;
}
public void deleteUser(User u) {
using (TransactionScope scope = new TransactionScope()) {
List<IJob> jobs = jobService.findAllByUser(u.Id);
foreach (IJob job in …Run Code Online (Sandbox Code Playgroud) c# dependency-injection circular-dependency inversion-of-control
与上一个问题相关,我现在有以下问题:
在接下来的风景中:
class B;
class A
{
// stuff, methods and so on
B b;
};
class B
{
// stuff, methods and so on
A a;
};
Run Code Online (Sandbox Code Playgroud)
这里我们在A和之间有一个循环依赖B,但是这个代码是不正确的,因为它B是一个不完整的类型.解决方案是B通过B例如智能指针的指针来改变.但是添加指针会不必要地增加复杂性和资源消耗,因为你不需要指针!
在上一个问题中,我试图通过模板避免使用指针,所以我在类定义了两个类的时候延迟了类的瞬间,但是我无法成功地完成它.
避免指针是不可能的吗?有没有众所周知的设计来避免循环依赖?
是否有可能避免在以下头文件循环依赖而不转动数据成员B1中A类的指针/引用,而不放松在直列式功能需求B类?
啊:
#ifndef A_H
#define A_H
#include <B.h> // Required, as data member b1 is not a pointer/reference
class A {
public:
B b1; // I want to keep this as as it is.
int m_a;
};
#endif
Run Code Online (Sandbox Code Playgroud)
Bh:
#ifndef B_H
#define B_H
#include <A.h> // Required, as f() calls a member function of class A
class B {
public:
int f(A &a){return a.m_a;} // I want this to be …Run Code Online (Sandbox Code Playgroud) 我models.py在两个不同的应用程序中有两个Django .
processor app models.py
from address.models import Address
...defining clasess
class Displayble(models.Model):
# It has no DB fields
Run Code Online (Sandbox Code Playgroud)
address app models.py
from processor.models import Displayable
class Address(models.Model, Displayble):
...some fields, stored in DB
Run Code Online (Sandbox Code Playgroud)
将Dispalyble类移动到另一个文件是解决此依赖关系的唯一选择吗?
c++ ×7
dependencies ×2
c# ×1
delphi ×1
delphi-units ×1
django ×1
header-files ×1
import ×1
include ×1
python ×1