我一直想知道,如果两次定义琐碎方法是好的还是坏的做法,这取决于
项目是在debug/release -state上.这是为了内联它们.例如,Foo.h:
class Foo
{
public:
...
const bool& IsBoolean() const;
private:
bool _boolean;
};
#ifndef _DEBUG
/** We're in release, so let's advice compiler to inline this...
*
*
*/
inline const bool& Foo::IsBoolean() const
{
return _boolean;
}
#endif
Run Code Online (Sandbox Code Playgroud)
现在,在Foo.cpp中:
#include "Foo.h"
...
#ifdef _DEBUG
/** We're debugging this, no need for inlining...
*
*
*/
const bool& Foo::IsBoolean() const
{
return _boolean;
}
#endif
Run Code Online (Sandbox Code Playgroud)
这完全没用吗?例如,由于编译器(MSVC)能够自行内联/优化方法?
然而,这是我多年来一直使用的东西.请纠正我,如果我在这里完全错了......
从来没有在asp.net中看到过这种情况,但从来没有这样,我可以定义函数而不是成为类的一部分吗?
我想要的是一个实用程序库.目前我有Utils类,每次我需要使用它来填充下拉列表,我必须创建和初始化Utils()对象......除了声明类静态之外,我还有什么方法可以解决这个问题当我访问会话时呢?
我使用的是c#,而不是VB.
谢谢
我有一个多边形类,它存储一个Microsoft.Xna.Framework.Vector2列表作为多边形的顶点.创建多边形后,我希望其他类能够读取顶点的位置,但不能更改它们.
我目前通过这个字段公开顶点:
/// <summary>
/// Gets the vertices stored for this polygon.
/// </summary>
public List<Vector2> Vertices
{
get { return _vertices; }
}
List<Vector2> _vertices;
Run Code Online (Sandbox Code Playgroud)
但是,您可以使用以下代码更改任何顶点:
Polygon1.Vertices[0] = new Vector2(0, 0);
Run Code Online (Sandbox Code Playgroud)
要么
Polygon1.Vertices[0].X = 0;
Run Code Online (Sandbox Code Playgroud)
如何限制其他类只能读取这些顶点的属性,而不能将新的顶点设置为我的列表?我唯一能想到的是将副本传递给请求它的类.
请注意,Vector2是一个结构,它是XNA框架的一部分,我无法更改它.
谢谢.
我有2个班级:DataObject
和DataElement
.DataObject
保持指针(只)DataElement
S,和一个DataElement
包含指向几种类型,其中一个DataObject
.
这曾经没有问题,因为我只使用指针DataObject
输入DataElement
,所以DataObject
在标题中的前向声明DataElement
就足够了.
但是,现在,我尝试添加一个析构函数DataElement
,我需要删除一个DataObject
.在这方面,编译器说:
dataelement/destructor.cc: In destructor ‘DataElement::~DataElement()’:
dataelement/destructor.cc:8: warning: possible problem detected in invocation of delete operator:
dataelement/destructor.cc:8: warning: invalid use of incomplete type ‘struct DataObject’
dataelement/dataelement.h:7: warning: forward declaration of ‘struct DataObject’
dataelement/destructor.cc:8: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined. …
Run Code Online (Sandbox Code Playgroud) c++ destructor class-design circular-dependency forward-declaration
以下程序可以成功运行:
class Simple(object):
def __init__(self, name):
self.name = name
def __add__(self, other):
c = Composite()
c._members.append(self)
c._members.append(other)
return c
def __repr__(self):
return "Simple('%s')" % self.name
class Composite(object):
def __init__(self):
self._members = []
def __add__(self, obj):
if isinstance(obj, Simple):
out = Composite()
out._members = [k for k in self._members] + [obj]
elif isinstance(obj, Composite):
out = Composite()
out._members = [k for k in self._members + obj._members]
else:
raise TypeError
return out
if __name__ == "__main__":
s1 = Simple('one')
s2 = Simple('two') …
Run Code Online (Sandbox Code Playgroud) python class-design forward-declaration method-resolution-order
我想做的事情非常简单.我有一个类来处理我的数据库执行调用clsSQLInterface
.此类包含一个静态函数bool isSQLSafe
,false
如果发送执行的SQL被认为是危险的,它将返回.这是我有一点我可以过滤掉任何恶意事件.
现在,我的程序的其他部分实际需要的能够做这样的事情UPDATE
,DELETE
等等.所以我想我会继承clsSQLInterface
类并重写isSQLSafe
的东西,总是返回功能true
.
这不是关于数据库安全性的问题btw!
好的,所以我这样做了......
public class clsSQLInterface //Code not shown, just definitions
{
private static string connectionString(string sKey){...}
public static bool isSQLSafe(string sSQL){...}
public static DataTable executeSQLText(string sSQL, string sConnectionKey){...}
public static DataTable executeGenericQuery(clsGenericSQL query,string sDBKey){...}
}
Run Code Online (Sandbox Code Playgroud)
而最重要的课程..
public class clsSQLInterface_unsafe : clsSQLInterface
{
public clsSQLInterface_unsafe()
{
}
public new static bool isSQLSafe(string sSQL) //overriding the base method
{ return true; }
} …
Run Code Online (Sandbox Code Playgroud) 我想在另一个类的另一个函数方法中运行一个类的函数方法.我编写了以下代码作为示例,它编译并返回预期值.我的问题是,这是否是在另一个类方法中预先形成类方法计算的正确方法...
问候
#include <iostream>
class CBoo {
public:
CBoo();
void Test();
void Plot();
private:
int b;
};
CBoo::CBoo() {
b = 3;
}
void CBoo::Test() {
b++;
}
void CBoo::Plot() {
std::cout << "b: " << b << std::endl;
}
class CFoo {
public:
CFoo();
void Test( CBoo &Boo );
void Plot();
private:
int a;
};
CFoo::CFoo() {
a = 3;
}
void CFoo::Test( CBoo &Boo ) {
for ( int i = 0 ; i < 10 ; i++ …
Run Code Online (Sandbox Code Playgroud) 我有一个对象,我在一个方法初始化,如:
public void something()
{
Dummy obj = Factory.getDummy();
method2(obj);
}
Run Code Online (Sandbox Code Playgroud)
现在,这个Dummy
对象将被许多方法使用
public void method2(Dummy obj)
{
method2(obj);
....
}
Run Code Online (Sandbox Code Playgroud)
现在,我怀疑这种情况必须如何处理,就像我这样做.或者,obj
在something
类级别字段中创建并初始化something
.
喜欢:
private Dummy obj;
public void something()
{
obj = Factory.getDummy();
method2(obj);
}
Run Code Online (Sandbox Code Playgroud)
并用于后续方法.(从方法中删除参数).
那么,处理这种情况的最佳方法是什么?为什么?
我正在使用Borland Builder C++.我有一个内存泄漏,我知道它必须是因为我创建的这个类,但我不知道如何解决它.请看我的代码 - 任何想法将不胜感激!
这是.h文件:
#ifndef HeaderH
#define HeaderH
#include <vcl.h>
#include <string>
using std::string;
class Header {
public:
//File Header
char FileTitle[31];
char OriginatorName[16];
//Image Header
char ImageDateTime[15];
char ImageCordsRep[2];
char ImageGeoLocation[61];
NitfHeader(double latitude, double longitude, double altitude, double heading);
~NitfHeader();
void SetHeader(char * date, char * time, double location[4][2]);
private:
void ConvertToDegMinSec (double angle, AnsiString & s, bool IsLongitude);
AnsiString ImageDate;
AnsiString ImageTime;
AnsiString Latitude_d;
AnsiString Longitude_d;
double Latitude;
double Longitude;
double Heading;
double Altitude;
};
Run Code Online (Sandbox Code Playgroud)
这是一些.cpp文件: …
我在ClassName.m中编写了一个实例方法:
-(void)methodName:(paraType)parameter
{...}
Run Code Online (Sandbox Code Playgroud)
并使用它来调用它
[self methodName:parameter];
Run Code Online (Sandbox Code Playgroud)将弹出警告,但代码仍然成功运行.
这是因为我还没有创建类的实例吗?为什么方法仍能正常运行?调用自我方法来阻止警告的正确方法是什么?
class-design ×10
c++ ×4
c# ×3
class ×3
asp.net ×1
destructor ×1
inheritance ×1
instance ×1
iphone ×1
java ×1
methods ×1
oop ×1
optimization ×1
overriding ×1
pointers ×1
python ×1
xna ×1