为什么c ++类不能为函数和数据成员使用相同的名称?
class demo{
public:
int size();
private:
int size;
};
int main(){
return 0;
}
C:\Users\S>g++ demo.c
demo.c:5:7: error: declaration of 'int demo::size'
demo.c:3:7: error: conflicts with previous declaration 'int demo::size()'
Run Code Online (Sandbox Code Playgroud) 我有两个问题 - (I)
code-fragment-1
if(<condition-statement>){
}
else if(<condition-statement-2>){
//statements-1
}
//statements-2
Run Code Online (Sandbox Code Playgroud)
代码片段-2-
if(<condition-statement>){
}
else{
if(<condition-statement-2>){
//statements-1
}
//statements-2
}
Run Code Online (Sandbox Code Playgroud)
以上两个代码片段是否相同?
(II)何时使用ifs(使用C++)?
是否存在与深度继承树(在c ++中)相关的任何效率缺点,即,一大组类A,B,C等,使得B扩展A,C扩展B,等等.我能想到的一个效率含义是,当我们实例化最底层的类时,比如C,那么也会调用B和A的构造函数,这将具有性能影响.
是否有算法在给定色调值(0度到360度)之间找出颜色的波长.MATLAB中是否有任何内置函数?
你是gyus看过那些基于flash的翻书.我想在Java中创建相同的东西.我正在使用JTextArea作为本书的叶子.我想知道的是如何通过覆盖与Mouse/KeyListener结合的componentPaint方法来实现页面翻转效果呢?
我有一个基于控制台(System.in和System.out基于)的Java独立应用程序.我试图将其转换为applet.但问题是Java applet中没有控制台!换句话说,当我写入系统输出时,它不会显示给用户,同样地,在没有控制台的情况下,我无法从用户那里获得输入.
我知道可以使用TextArea和完成控制台外观KeyListener,但为此我需要两个不同的组件,一个用于输入,一个用于输出.我希望输入和输出进入同一个组件.有任何想法吗?
在C++中,假设我有一个具有以下构造函数的Person -
Person::Person(const string& nm, const string& id)
{
name = nm;
idNum = id;
}
Run Code Online (Sandbox Code Playgroud)
现在,我还有一个名为Student的Person子类,其中包含额外的数据成员major和gradYear.是否有必要让Student类的构造函数在初始化列表中调用基类Person的构造函数,如果是,为什么?
Student::Student(const string& nm, const string& id, const string& maj, int year)
: Person(nm, id){
major =maj;
gradYear =year;
}
Run Code Online (Sandbox Code Playgroud)
我不能像这样定义学生的构造函数 -
Student::Student(const string& nm, const string& id, const string& maj, int year)
{
Person(nm, id);
major =maj;
gradYear =year;
}
Run Code Online (Sandbox Code Playgroud) #include "PQueue.h"
struct arcT;
struct coordT {
double x, y;
};
struct nodeT {
string name;
coordT* coordinates;
PQueue<arcT *> outgoing_arcs;
};
struct arcT {
nodeT* start, end;
int weight;
};
int main(){
nodeT* node = new nodeT; //gives error, there is no constructor
}
Run Code Online (Sandbox Code Playgroud)
我的目的是nodeT在堆中创建一个新的.错误是:
错误C2512:'nodeT':没有合适的默认构造函数可用
假设我有:
class B;
class A{
private:
B *b;
public:
bar(){ b->foo()};
foo();
}
class B{
private:
A *a;
public:
bar(){ a->foo();}
foo();
}
Run Code Online (Sandbox Code Playgroud)
编译时,这个文件给出了一个
错误"无效使用不完整类型struct B",
即使我已经向前声明了B类.据我所知,这是因为当我调用函数foo()时b,编译器仍然不知道存在这样的函数.我怎么解决这个问题?
在main.cpp文件中...
#include "pqueue.h"
struct nodeT;
struct coordT {
double x, y;
};
struct arcT {
nodeT *start, *end;
double weight;
};
int arcComp(arcT *arg0, arcT *arg1){
if(arg0->weight == arg1->weight)
return 0;
else if(arg0->weight > arg1->weight)
return 1;
return -1;
}
struct nodeT {
coordT* coordinates;
PQueue<arcT *> outgoing_arcs(arcComp); // error on this line
};
Run Code Online (Sandbox Code Playgroud)
在文件pqueue.h中...
#ifndef _pqueue_h
#define _pqueue_h
template <typename ElemType>
class PQueue
{
private:
typedef int (*CallbackFunc)(ElemType, ElemType);
CallbackFunc CmpFunc;
public:
PQueue(CallbackFunc Cmp);
~PQueue();
};
#include "pqueue.cpp"
#endif …Run Code Online (Sandbox Code Playgroud) C++ 中的普通函数(而不是静态函数)是否只有一个类的副本,这意味着所有对象都引用同一个副本。如果是这样,普通函数和静态函数有什么区别?另外,我的老师说我们不能使用“(object_name).(func_name)”表示法调用类的静态函数,而必须使用“class_name::(func_name)”来调用它,而我明白这是逻辑上的要做的事情,但就语言而言,我是否不允许使用 (object_name).(func_name) 表示法调用静态函数?最后,我是否可以在类的构造函数中或该类的任何其他函数内初始化类的静态数据成员,而必须从类外部执行此操作?
c++ ×8
java ×2
colors ×1
constructor ×1
if-statement ×1
inheritance ×1
matlab ×1
new-operator ×1
oop ×1
performance ×1
swing ×1