b*********n 发帖数: 1258 | 1 C++ Prototype
==============================
class Ball
{
public:
enum BallColor { RED, BLUE };
BallColor Color() const { return _color; }
private:
BallColor _color;
// Other data in class (unrelated to assignment)
};
我想在主函数里面定义一个Ball array,每一个元素都是一个Ball type
如果我不修改Ball class,可以初始化那个Ball array吗?
我的想法是修改Ball class的ctor
或者定义一个method来初始化Ball array里面的每一个元素
但是不知道,如果不修改这个class,可不可以实现同样的目的
谢谢 |
|
w****g 发帖数: 44 | 2 1. Write a Java method that, given a square matrix of integers, returns
true if there is at least one row, column, or diagonal that is all zero. The
signature should be:
class MatrixZeroTest {
enum TestType { Row, Column, Diagonal }
public static boolean allZeros(int[][] matrix, TestType type);
} |
|
h*******n 发帖数: 2052 | 3 比如我一个程序, 用Stack, Queue, Stack.h 和 Queue.h里面都#include Itemtype.h
但是Itemtype.h里面定义了一些变量, 比如
const int MAX_ITEMS = 5
enum RelationType {LESS, GREATER, EQUAL} 等等,
在主程序中#include Stack.h和Queue.h, 编译时出错, 说multiple declaration of
MAX_ITEMS还有RelationType, 这种问题怎么解决? |
|
E*****7 发帖数: 128 | 4 struct MyTry {
enum BB { 0, Red};
}; |
|
e****d 发帖数: 333 | 5 试试这个,有点笨,但是in place。
不知道能不能满足要求。
VC下编译通过。
#include
#include
using std::cout;
using std::endl;
using std::swap;
int main(){
enum {N=35};
char a[N]=" ab cd efg h i jk lmn op qr";
for(int k=0;k
cout<
}
cout<
int flag=0;
int i=0;
int j=0;
while(j
while(j
if(a[j]!=' '){
flag=0;
swap(a[i],a[j]);
i++;
j++;
}
|
|
s********h 发帖数: 286 | 6 如果定义一个enumerated constant,比如这个:
enum {jack, diomand, heart, spade};
然后定义一个变量:
int suit = spade;
然后我输出suit的话,如果用
cout << suit;
结果就会是 3 ,可是我想让它输出的是 string “spade”,应该怎么做呢?
谢谢先! |
|
b********r 发帖数: 1080 | 7 笨办法.
enum xxx{jack, diomand,...};
char* card[]={"jack", "diomand", ......};
int suit=spade;
cout< |
|
s******n 发帖数: 21 | 8 Does C++ 0x enum class provide any elegant solutions? C# is way better
when dealing with enumerations…. |
|
t****t 发帖数: 6806 | 9 we are talking about enum, not arbitrary class. of course nothing is
impossible (given you can implement with anything else), but there is
difference between elegent solution and ugly solution. |
|
z****e 发帖数: 2024 | 10 class Count {
enum { SZ = 100 };
int n[SZ];
public:
void increment() {
for(int i = 0; i < SZ; i++)
n[i]++;
}
};
class Incrementer : public Runnable {
Count* count;
public:
Incrementer(Count* c) : count(c) {}
void run() {
for(int n = 100; n > 0; n--) {
Thread::sleep(250);
count->increment();
}
}
};
int main() {
Count count;
Thread t0(new Incrementer(&count));
Thread t1(new Incrementer(&count));
}
以上代码产生
segmentation fault!
我不是很懂,据说是count被销毁了 |
|
j***i 发帖数: 1278 | 11 看c++ template数书上有段
Types that involve unnamed class types or unnamed enumeration types cannot b
e template type arguments (unnamed classes or enumerations that are given a
name through a typedef declaration are OK).
An example illustrates these two exceptions:
template class List {
…
};
typedef struct {
double x, y, z;
} Point;
typedef enum { red, green, blue } *ColorPtr;
int main()
{
struct Association
{
int* p;
int* q;
};
List e |
|
r****t 发帖数: 10904 | 12 enum type 也还是没有 named 啊。
cannot b
given a |
|
X****r 发帖数: 3557 | 13 Err.. they are totally different things. Operators are special
non-alphabetic symbols with their predefined syntax and semantics,
and take one or more operand to form expressions. Functions are
units of code that has its own declaration, body, scope, etc.,
and can be called using the function call operator ().
The only connection between them is that for overloadable operators,
you can define functions with the name "operator @" where @ is the
operator symbol for your own class or enum to overlo... 阅读全帖 |
|
s********z 发帖数: 5411 | 14 新手,请原谅!
我build的时候遇到两个这个error,“expected class name before "(" token”
第一个发生在 ~CDigiCam(); 这条line上。
第二个发生在 ~CCamLinkCam();
谁能帮我看看吗? 多谢!
第一个h file。
#pragma once
#ifndef CDIGICAM_H
#define CDIGICAM_H
#include
#include
#include
typedef unsigned char byte; /* 8-bit */
typedef unsigned short word; /* 16-bit */
class CDigicam
{
public:
CDigicam();
~CDigiCam(); *********这条line的error
int iXCamRes,
iYCamRes,
.... 阅读全帖 |
|
g*********s 发帖数: 1782 | 15 i figure out the following formula to determine the access level
transition. can daniu confirm its correctness?
enum ACC_LEV {
PRIVATE,
PROTECTED,
PUBLIC
};
derived.dat.access_level = min(base.data.access_level,
inheritance.access_level);
so if B::dat is private, and base class B is public inherited by derived
class D, then B::dat in D is still private, and thus inaccessible by D
and its descents.
however, if dat is public while B is private inherited by D, then B::dat
is now private... 阅读全帖 |
|
O*******d 发帖数: 20343 | 16 说一下我怎么做的antialiase。 先把图形计算好,比最后的图像多出宽三列高三行。
图形是一个浮点数的点阵。 两个相邻的点之间的距离是一个单位。 然后用三次方程插
值。每个插值要用图形中4X4的值来计算。 在四个点组成的方格中均匀插入5X5=25个点
(单核机)或7X7=49个点(多核机),给每个插值赋予颜色,最后把一个方格中所有的
颜色平均,就是一个像素的颜色。
由于一组插值用的是同一套三次方程系数,所以写了一个class来做。 其实是一个
template。 做插值时,有两个套着的循环,分别在X方向移动和Y方向移动。 内循环是
在Y方向从上往下移动,这样每次移动一步,只需要计算最后一列的三次方程系数。下
边是我写的Cubic Interpolator. 是一个recursive template。For bicubic
interpolation, N = 2. 所有的重复计算都尽可能的避免了。 这个templete的计算
速度,在N==2时,是单独bicubic interpolation的速度的三倍。
template
class Cu... 阅读全帖 |
|
G****A 发帖数: 4160 | 17 The question is inspired by Item 38 in <>. Given
following code,
*****************
enum ShapeColor { RED, GREEN, BLUE };
class Shape {
public:
virtual void draw(ShapeColor color = RED) const = 0;
...
};
class Rectangle: public Shape {
public:
//definition of draw() goes here.
...
};
********************
How to define the draw() in Rectangle so that it comes with a default
parameter value other than RED?
NOTE: the article did mention that a definition like this will not wor... 阅读全帖 |
|
l******d 发帖数: 530 | 18 volume 1, 2nd edition, p. 719 -720
对下面代码不是很懂。
IntStack只有一个constructor
IntStack() : top(0) {}
没有parameter,那么这句
IntStackIter(IntStack& is) : s(is), index(0) {}
initializer list里s(is)是怎么被调用的?(s是IntStack类型的reference)
书中代码如下:
//: C16:IterIntStack.cpp
// Simple integer stack with iterators
//{L} fibonacci
#include "fibonacci.h"
#include "../require.h"
#include
using namespace std;
class IntStack {
enum { ssize = 100 };
int stack[ssize];
int top;
public:
IntStack() : top(0) {}
void push(i... 阅读全帖 |
|