由买买提看人间百态

topics

全部话题 - 话题: endles
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
b***y
发帖数: 2799
1
☆─────────────────────────────────────☆
yapple (Fedora) 于 (Tue Jul 12 17:47:25 2005) 提到:
code is:
#include
using namespace std;
class B
{
public:
B()
{
cout << "construct a new B" << endl;
}

~B()
{
cout << "destruct a B object" << endl;
}
void print()
{
cout << "Hello, I am B!" << endl;
}

};
class A
{
public:
void func()
{
static B* pb;
if(!pb)
{
pb = new B();
}
b*****d
发帖数: 23
2
来自主题: Programming版 - C++ (direct vs indirect initialization)
The codes following yields results:
ctor
non-const
const
Which I think it is wrong.
It should be
ctor
non-const
non-const
const
////////////////
using namespace std;
class A
{
public:
A(const A&){ cout << "const" << endl;};
A(A&){cout << "non-const" << endl;};
A(){cout << "ctor" << endl;};
static const long i = 1;
static double k;
};
double A::k;
int _tmain(int argc, _TCHAR* argv[])
{
A a = A();
A& b = a;
const A& d = b;

A c(b);
A e(d);
return 0;
}
r*********l
发帖数: 117
3
一个实验copy-constructor的程序:
#include
using namespace std;
#define PR(VA) cout<<#VA" = "< class X{
int i,j,k;
public:
X(int ii=0,int jj=0,int kk=0):i(ii),j(jj),k(kk){}
X(const X& xx){cout<<"copy constructor"< X& operator=(const X& xx){cout<<"operator ="< .k*2;return *this;}
void set(int ii,int jj,int kk){i=ii;j=jj;k=kk;}
void print()const{PR(i);PR(j);PR(k);}
};
void func1(X x){x.print();}
X func2(){
r*********r
发帖数: 3195
4
来自主题: Programming版 - 问一道C++面试题
#include
#include
using std::cout;
using std::endl;
using std::string;
string process(const string& s)
{
string r;
for(int i=0; i if( s.substr(i,3) == "xxx" ) {
i += 2;
r += 'A';
}
else if( s.substr(i,3) == "xMx") {
i += 2;
r += 'B';
}
}
return r;
}
int main()
{
cout << process("xxMxMxxxMxxx") << endl
<< process("xxxMxMxxxxMMxMxMx") << endl;
return 0;
}
b***y
发帖数: 2799
5
来自主题: Programming版 - [合集] a simple c++ puzzle
☆─────────────────────────────────────☆
wenchang (随缘@菩提树下,拈花一笑@求道...) 于 (Sun Sep 25 19:33:16 2005) 提到:
how can we make the output of
#include
main(){
cout<<"hello world"< }
as the following:
initialize
hello world
clean up
without changing the main program.
☆─────────────────────────────────────☆
acrof (KoKo) 于 (Sun Sep 25 19:58:06 2005) 提到:
#include
class A
{
public:
A()
{
cout<<"initialize"< }
~A()
{
cout<<"clean up"< }
}
r**e
发帖数: 339
6
来自主题: Programming版 - c++之极弱问
#include
using namespace std;
int main()
{
//part 1 below
int sum=0,v1;
while(cin>>v1)
{
sum+=v1;
}
cout<< "sum is "<< sum< //part 2 below
cout<< "imput v2 "< int v2=0;
cin>>v2;
cout< return 0;
}
第一部分读入多个数,然可求和.用字母或 ctrl+z 做结束符时,第二部分没有执行读
入v2,直接输出v2结束,返回零.为啥啊?
j*****a
发帖数: 436
7
来自主题: Programming版 - c++之极弱问
#include
using namespace std;
int main()
{
//part 1 below
int sum=0,v1;
while(cin>>v1)
{
sum+=v1;
cin.get(); //get the space or the return character
}
cin.clear(); // clear out the non-numeric character
cin.get(); // get the return character
cout<< "sum is "<< sum< //part 2 below
cout<< "imput v2 "< int v2=0;
cin>>v2;
cout< return 0;
}
a*****n
发帖数: 149
8
来自主题: Programming版 - c++ question
刚写了小代码,发现一点疑惑
template
int compare(T &a1, T &a2)
{
if (a1 < a2)
return 1;
else
return 2;
}
void main()
{
cout << compare("abc", "def") << endl;
if ("abc" < "def")
cout << 1 << endl;
else
cout << 2 << endl;
}
第一个输出是2,第二个输出1。为啥呢?多谢赐教!
d*******n
发帖数: 524
9
来自主题: Programming版 - 为什么foo1可以而foo2不行?
程序在后面。
我的理解是“Hello World”是内存里一段const char[],
foo2之所以不行是因为除了foo2这个function之后,这个char 的array就被release了。
但foo1难道不是相同的情况吗?为什么
cout << foo1() < 这一行就可以输出Hello World呢?
请指教
#include
#include
using namespace std;
char* foo1();
const string& foo2();
int main(int argc, _TCHAR* argv[])
{
int i;
cout << foo1() < cout << foo2() << endl;
cin >> i;
return 0;
}
char* foo1()
{
return "Hello World";
}
const string& foo2()
{
return "Hello World";
}
s*******e
发帖数: 664
10
来自主题: Programming版 - [合集] 请教一个c++问题
☆─────────────────────────────────────☆
GodBlessMe (GodBlessMe) 于 (Sat Nov 7 17:30:51 2009, 美东) 提到:
void f1(char*& p)
{
p="a";
}
void f2(char *& p)
{
*p='a';
}
int main()
{
char s[]="1234";
char *p=s;
f2(p);
cout< f1(p);
cout< cout< return 0;
}
a234
a234
a
我没有闹明白为什么为什么char*&调用的时候
能对字符修改,却不能赋予新的字符串呢?
我发现f1赋值后;p的reference地址就变了呢
我翻了基本书,也没找到答案。哪位帮解释解释
☆─────────────────────────────────────☆
GodBlessMe (GodBlessMe) 于 (Sat Nov 7 17:34:23 2009, 美东) 提到:
还有f1(s)也是编译
r******s
发帖数: 925
11
来自主题: Programming版 - C++ question (reference and pointer)
The code in below doesn't work. What's the best way to fix it without
changing define of A, B, C?
what's the output?
#include
#include
using namespace std;
typedef char TT[10];
int B (TT &b)
{
cout << b << ", Len: " << strlen(b)< }
int A (TT a)
{
cout << a << ", Len a : " << strlen(a) << endl;
B(a);
}

int C (char c[10])
{
cout << c << ", Len c : " << strlen(c) << endl;
}
int main()
{
TT d;
cin >> d;
A(c);
C(d);
return 0;
}
p*****u
发帖数: 711
12
#include
class A {
public:
void print();
};
void A::print() {
cout << "A.print"<< endl;
}
class B: publicA {
}
int main() {
B obj;
obj.print();
return 0;
}
output is "A.print"
接下来
#include
class A {
public:
virtual void print();
};
void A::print() {
cout << "A.print"<< endl;
}
class B: publicA {
}
void B::print(){ count << "B.print" << endl;}
int main() {
B obj;
obj.print();
return 0;
}
error: no 'void B::print()' member function declared in class 'B'
如果想ov... 阅读全帖
C**m
发帖数: 126
13
来自主题: Programming版 - c++ singleton questions
#include
#include
class X {
public: X* get_X();
private:
static X _x;
};
X X::_x;
X* X::get_X()
{
return &_x;
}
int main(int argc, char* argv[])
{
X *p;
std::cout << "the address " << (void *) p->get_X() << std::endl;
X a;
std::cout << "the address " << (void *) a.get_X() << std::endl;;
X b;
std::cout << "the address " << (void *) b.get_X() << std::endl;;
return 0;
}
/... 阅读全帖
C**m
发帖数: 126
14
来自主题: Programming版 - c++ singleton questions
#include
#include
class X {
public: X* get_X();
private:
static X _x;
};
X X::_x;
X* X::get_X()
{
return &_x;
}
int main(int argc, char* argv[])
{
X *p;
std::cout << "the address " << (void *) p->get_X() << std::endl;
X a;
std::cout << "the address " << (void *) a.get_X() << std::endl;;
X b;
std::cout << "the address " << (void *) b.get_X() << std::endl;;
return 0;
}
/... 阅读全帖
x******g
发帖数: 41
15
来自主题: Programming版 - 问个内存的问题
void testAddressOf()
{
int a = 4;
int *b = &a;
int c = 5;
cout << "address of a: " << &a << endl;
cout << "address of &b " << &b << endl;
cout << "address of c: " << &c << endl;
}
三个地址分别是
0015F75C
0015F750
0015F744
整形不是4个字节么,为什么地址之间差距是0xC=12呢?
H****r
发帖数: 2801
16
来自主题: Programming版 - question about c++ constructor

这个 "C++ temporary object can not be bound to non-const reference" 俺似乎理解
了,但是楼主的例子在VS2010 下并没有报错啊?
class M{
public:
int i;
public:
M(){ std::cout << "default constructor" << std::endl;}
M(int ii):i(ii){ std::cout << "constructor"<< std::endl;}
M(M & mm):i(mm.i){ std::cout << "copy constructor"<< std::endl;}
};
int main()
{
M mm = 1;
}
其中 M(M & mm):i(mm.i) 改成 M(const M & mm):i(mm.i) 并没发现对 M mm = 1; 有
任何影
响啊。
H****r
发帖数: 2801
17
来自主题: Programming版 - question about c++ constructor

甚至写成这样都可以编译运行啊(VS 2010):
#include
class M{
public:
int i;
public:
M(){ std::cout << "default constructor" << std::endl;}
M(int ii):i(ii){ std::cout << "constructor"<< std::endl;}
M(M& mm):i(mm.i){ std::cout << "copy constructor"<< std::endl;}
};
int main()
{
M mm(M(1));
}
g*********s
发帖数: 1782
18
how does the compiler know if the 1st get() or the 2nd get() should be
called?
#include
using namespace std;
class X {
public:
X(int x = 0): dat(x) {}
void print() const { cout << dat << endl; }
void inc() { dat ++; }
private:
int dat;
};
class Y {
public:
Y(const X& x): dat(x) {}
Y(const Y& y) { dat = y.get(); }
X& get() { cout << "ref" < const X& get() const { cout << "const ref" << endl; return dat;
}
private:
X dat;
};
int main()
{
X x(10);
Y y(x);
Y y2(y);
y.g... 阅读全帖
a***y
发帖数: 2803
19
#include
using namespace std;
int main() {
int a[5];
a[0]=9;
a[1]=8;
a[2]=7;
a[3]=6;
a[4]='\0';
int i=1;
a[i++]=i;
a[i]=i++;
cout << "a[0] is "<< a[0] << endl;
cout << "a[1] is "<< a[1] << endl;
cout << "a[2] is "<< a[2] << endl;
return 0;
}
运行结果是
a[0] is 9
a[1] is 1
a[2] is 2
a[i++]=i;语法正确,而且运行也正确啊.

m
e
undefined
N**********d
发帖数: 9292
20
来自主题: Programming版 - 问个缺少逗号的数组赋值问题
源程序是:
#include
#include
using namespace std;
int main(int argc, char * argv[])
{
std::string m_ColumnName [] =
{
"str1",
"str2"
"last_one"
};
cout << m_ColumnName[0].substr(0,4) << endl;
cout << m_ColumnName[1].substr(0,4) << endl;
cout << m_ColumnName[2].substr(0,4) << endl;
return 0;
}
赋值的时候,"str2"后面少了个逗号,然后"last_one"到哪里去了?
是不是g++直接就把它扔了?
前两行输出都是预期的
str1
str2
而第三行则不确定,经常产... 阅读全帖
A**u
发帖数: 2458
21
来自主题: Programming版 - 请教pthread producer-consumer问题
照书编了一个,
结果总是不对,请大牛看看问题出在哪里呢
#include
#include
#include
pthread_mutex_t region_mutex = PTHREAD_MUTEX_INITIALIZER;
sem_t items;
int b; /* buffer size = 1; */
void add_buffer(int i){
b = i;
}
int get_buffer(){
return b ;
}
void *producer(void*)
{
int i = 0;
std::cout <<"I'm a producer" << std::endl;
while (1) {
pthread_mutex_lock(®ion_mutex);
add_buffer(i);
pthread_mutex_unlock(®ion_mutex);
sem_post(&ite... 阅读全帖
x******a
发帖数: 6336
22
来自主题: Programming版 - STL的问题(C++)
抄了cplusplus上面的一个小程序,g++是提示下面的错误,请问怎么回事?谢谢
min.cpp:2:2: error: invalid preprocessing directive #inlcude
name's-MacBook-Pro:~ name$ emacs min.cpp
#include
#inlcude
using namespace std;
int main()
{
cout<<"min(1,2)"<< min(1,2)< cout <<"min(1, 3.5)"<< min(1.0, 3.5)< cout<< "min(abcd, adccd)"<< min('a', 'b')< return 0;
}
X*4
发帖数: 101
23
来自主题: Programming版 - 这个dtor为啥能被调用呢
关于boost shared_ptr的,
#include
#include
#include
class A{
public:
virtual void sing() = 0;
protected:
virtual ~A(){
std::cout << "dtor of base" << std::endl;
}
};
class B: public A{
public:
virtual void sing()
{
std::cout << "DO re mi fa so la" << std::endl;
}
~B(){
std::cout << "Dtor of Derived " << std::endl;
}
};
boost::shared_ptr create(){
boost::shared_ptr
p(new B());
...
阅读全帖
g***l
发帖数: 2753
24
指向子类对象的基类指针能访问子类中重载的虚函数,而不能访问其私有函数。
比如
class CBase
{
public:
CBase();
virtual void Print(){cout<<"base"< };
class CDerived: public CBase
{
public:
CDerived();
virtual void Print(){cout<<"Derived"< void Personal(){cout<<"Derived private owned"< }
1. int main()
2. {
3. CDerived oderived;
4. CBase* pbase=&oderived;
5. pbase->Print();
6. pbase->Personal();
7. }
编译的时间,error: ‘class CBase’ has no member named ‘Personal’in line 6.
我的问题是,
1. 如果是因为pbas... 阅读全帖
e********r
发帖数: 2352
25
来自主题: Programming版 - 请教一个C++的问题
为了使用try ... throw ... catch的方式处理异常,写了以下一段程序,就是读取一
个文件
ifstream file;
file.exceptions(ifstream::failbit | ifstream::badbit);
try{
file.open("./file");
string str;
while(getline(file, str))
{
cout< }
}
catch(ifstream::failure e)
{
cout<<"Return information: "< }
file.close();
请问为什么总是会执行catch... 阅读全帖
n***s
发帖数: 287
26
我需要写个简单的C++程序test.cpp, 如
int main() {
int a;
double b;
string c;
// do something about a, b, c
cout << a << endl;
cout << b << endl;
cout << c << endl;
return 0;
}
比如编译之后的executable叫test.out,
然后需要在一个bash script中运行test.out, 我想在bash中定义
三个变量A, B, C, 运行完test.out之后就立即把abc的值赋给ABC,
两个问题:
how to make variable B store a floating point number ??
how to pass values of a,b,c to A,B,C in bash ??
我bash比较外行,不知道这个该怎么做?或者cpp程序也要做相应
调整?还请高人指点,包子答谢!
b*****n
发帖数: 2324
27
来自主题: Programming版 - 这该不该算std::map的一个bug
std::cout << "3.5 map1.size(): " << map1.size() << endl;
std::cout << "4 map1[key1].size(): " << map1[key1].size() <<
endl;
std::cout << "4.5 map1.size(): " << map1.size() << endl;
output:
3.5 map1.size(): 0
4 map1[key1].size(): 0
4.5 map1.size(): 1
l********a
发帖数: 1154
28
来自主题: Programming版 - One c++ non-type template question
不太了解你的需求
如果只是针对int类型有特殊操作,其他类型都调用模板函数,你可以明确对于int的set
函数(即使有同名template set() 存在),我目前项目有个类似的情况就
是这样解决的.
网上说这跟c++的函数名称搜索次序有关,我忘记在哪里看到的了
测试代码:
void set(int t)
{
std::cout << "print from normal function 1\n" << t << std::endl;
}
void set(string t)
{
std::cout << "print from normal function 2\n" << t.c_str() << std::endl;
}
template
void set(T t)
{
std::cout << "print from template function\n" << t << std::endl;
}
int main()
{
int it = 10;
float ft = 10.0;
double dt = 20.53;
... 阅读全帖
h*****f
发帖数: 248
29
来自主题: Programming版 - Re: L 电面 (转载)
【 以下文字转载自 JobHunting 讨论区 】
发信人: henrysf (Henry), 信区: JobHunting
标 题: Re: L 电面
发信站: BBS 未名空间站 (Thu Oct 18 01:46:20 2012, 美东)
我写了以下的code, 之后就被拒了, 回信是说别的candidate写得比较好, 我还没看出
问题.
Additional requirements:
- The only operators you need to implement are +, -, *, and /.
However, your implementation should make it relatively easy to
add more operators.
- The calculator should accept floating point numbers and output
floating point numbers (i.e. when the input is 2 3 / the result
is 0.66667).
- ... 阅读全帖
y**b
发帖数: 10166
30
来自主题: Programming版 - 请问释放容器内存的方法
linux下面我直接用ps或top命令查看的,可以清楚看到各个进程的内存消耗。
swap这个方法我用google搜索到的,似乎是通用的一个trick; effective stl里面也有
提到。
下面是源码,只有swap之后进程的内存消耗才降到近乎0(getchar的时候察看)。
#include
#include
#include
int main () {
std::vector vec;
vec.resize(10000000,-1);
std::cout << "resize finished." << std::endl;
getchar();
vec.clear();
std::cout << "clear finished." << std::endl;
getchar();
std::vector().swap(vec);
std::cout << "swap finished." << std::endl;
getchar();
ret... 阅读全帖
s*w
发帖数: 729
31
来自主题: Programming版 - how to destruct list with loop?
#include
using namespace std;
struct node {
int val;
node* next;
node(int v):val(v),next(NULL) {}
};
struct sll {
node* head;
sll():head(NULL) {}
~sll() {
cout << "sll destructing starts here" << endl;
node* p = head;
while(p) { // i am having problems destrucing here with looped list
node* todel = p;
p = p->next;
delete todel; // i thought delete make todel NULL so it breaks loop
todel = NULL; // does this help? nono... 阅读全帖
m********r
发帖数: 334
32
来自主题: Programming版 - 问个copy constructor的问题
class example {
public:
example(string str){
cout <<"ctor with string "< example() {
cout<<"ctor without string" < example( example& rhs){
cout<<"copy ctor "< };
int main (int argc, char* argv[])
{
string str = "123456789";
example e1(str);
example e2 = str;
example e3 = e1;
example e4 = example();
return 0;
}
这个例子来自C++Primer,在VS2010可以编译,gcc下e2 and e4报错:no matching
function for call to example::example(example)
如果在copy constr... 阅读全帖
a**e
发帖数: 64
33
来自主题: Programming版 - C++ 11问题:emplace_back()
有一点很有意思,虽然编译的时候要求有copy或者move constructor,但是实际上并不
一定会使用。比如运行下面的例子:
class Person {
public:
Person(string name): name_(name)
{ cout << "in normal constructor" << endl; }
Person(const Person&)
{ cout << "in copy constructor" << endl; }
Person(Person&&)
{ cout << "in move constructor" << endl; }
private:
string name_;
};
int main()
{
vector persons;
persons.emplace_back("George");
};
output:
in... 阅读全帖
d****i
发帖数: 4809
34
The principle of polymorphism is the same. Consider the following equivalent
C++ code, it yields the same results. You can even better change to: Base *
a = new Derived(); and the results are the same.
#include
using namespace std;
class Base {
private:
int x;
public:
Base() : x(1) {}
virtual ~Base() {};
void showX1() {
cout< }
virtual void showX2() {
cout< }
};
class Derived : public Base {
private:
int x;
public:
... 阅读全帖
k**********g
发帖数: 989
35
来自主题: Programming版 - 问个指针array 的简单问题

Wrong code, not sure why it pass compilation and/or doesn't crash. Doesn't
compile on my machine.
If you want them to point to strings, use
int main()
{
const char* p[3] = { "a", "b", "c" };
std::cout << p[0] << std::endl;
std::cout << p[1] << std::endl;
std::cout << p[2] << std::endl;
return 0;
}
Notice double quote, because each is a string literal.
Q**g
发帖数: 183
36
像是经典的Double dispatch pattern.
你大概是想 Dog process DogData, Cat process CatData; 当 Dog 遇上 CatData 或者
Cat 遇上 DogData 就用缺省的 Animal process AnimalData。试试
#include
class Animal;
class Data;
class Cat;
class CatData;
class Dog;
class DogData;
using namespace std;
class Data {
public:
virtual void process(const Animal* animal) const {
cout << "Default Data and Animal" << endl;
}
virtual void process(const Cat* animal) const {
process((const Animal*) animal);
}
... 阅读全帖
f******5
发帖数: 11
37
//Algorithm DPKnapsack(w[1..n], v[1..n], W)
// var V[0..n,0..W], P[1..n,1..W]: int
// for j := 0 to W do
// V[0,j] := 0
// for i := 0 to n do
// V[i,0] := 0
// for i := 1 to n do
// for j := 1 to W do
// if w[i]  j and v[i] + V[i-1,j-w[i]] > V[i-1,j] then
// V[i,j] := v[i] + V[i-1,j-w[i]]; P[i,j] := j-w[i]
// else
// V[i,j] := V[i-1,j]; P[i,j] := j
// return V[n,W] and the optimal subset by ba... 阅读全帖
r*****t
发帖数: 286
38
来自主题: Quant版 - [合集] 问一个很弱的C++问题
☆─────────────────────────────────────☆
ggyygs (ggyygs) 于 (Thu Jun 7 11:36:17 2007) 提到:
其中加???那两行有啥用啊.直接用pp[i]=new int [n];不就可以了吗?
int **pp;
int m,n;
cout<<"m= "< cin>>m;
cout<<"n= "< cin>>n;
pp=new int *[m];
for(int i=0;i ??? if((pp[i]=new int [n])==NULL)
??? exit(0)
cout<<"input values"< for(i=0;i {
for(int j=0;j cin>>pp[i][j];
}
for(i=0;i {
for(
w*******x
发帖数: 489
39
对,以(i,j)为右下角的最大矩阵(X,Y)。假设X轴向右,Y轴向下。
基本上就是f(i,j).Y = min( f(i-1,j).Y+1, f(i,j-1).Y),
f(i,j).X = min(f(i-1,j).X+1 加上点特殊情况。写的有点乱
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int N=20;
typedef pair XY;
XY dp(bool A[][N+1], XY maxsub[][N+1])
{
XY x0;
int area=-1;
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
{
... 阅读全帖
b******b
发帖数: 713
40
when I interview someone have c++ on his resume, i usually start with
writing a line of code:
cout << "hollo world" << endl;
Q1:
what's "endl"? is it keyword, class name, variable name, method name, etc?
Q2:
why you can put "endl" after "<<"? what's "<<" in that line of code? to
support that syntax, what need to be done?
this usually is a good starting point to further talk about the c++ language.
h********a
发帖数: 1120
41
来自主题: Military版 - 犹太人问题最终解决方案
最终解决方案(德语:Die Endlösung)是第二次世界大战期间纳粹德国针对欧洲
犹太人的系统化的种族灭绝的计划及其实施,并导致最后的、最致命的最終解決方案(
Shoah)阶段。阿道夫·希特勒把它称作:“犹太人问题的最终解决方案” (Endl&
ouml;sung der Judenfrage)。
http://zh.wikipedia.org/wiki/%E6%9C%80%E7%BB%88%E8%A7%A3%E5%86%
那个年代的名词 真心气贯长虹
s*****r
发帖数: 773
42
来自主题: JobHunting版 - amazon的那道题目
看了网上的讨论, 关于amazon那道wood steel table chair furniture的题目
自己写了一个, 牛人帮我看看我是否写的有问题......
在我机器上编译通过, 运行成功.
#include
using namespace std;
class stuff {
public:
stuff() {}
virtual ~stuff() {}
virtual void info() = 0 ;
};
class table : public stuff {
public:
table() {}
~table() {}
void info() { cout<<"Table "< };
class chair : public stuff {
public:
chair(){}
~chair(){}
void info() {cout<<"Chair"<
c********u
发帖数: 18
43
来自主题: JobHunting版 - 一道面试的选择题
c 么?
因为change destructor into virtual 可能影响derived class的一些使用
例如 :
class A
{
public:
A(){};
~A(){cout<<"destructor A"< };
class B: public A
{
public:
B(){};
virtual ~B(){cout<<"destructor B"< };
void main()
{
A* p=new B();
delete p;
}
This will result in:
destructor A;
But if we declare ~A() as virtual ~ A(), then the result will be:
destructor B;
destructor A;
所以,如果B的destructor析构了B的一些成员变量的话,在非virtual的情况下,这些
成员变量不会被析构掉,但是在virtual的情况下会。
i********s
发帖数: 133
44
来自主题: JobHunting版 - MS interview question
#include
int main(int argc, char **argv)
{
int num;
std::cout << "enter the size of the square:";
std::cin >> num;
std::cout << std::endl << "size =" << num << std::endl;

int (*array)[num] = new (int[num][num]);
int t = 0;
for(unsigned i=0; i for(unsigned j=0; j array[i][j] = ++t;
unsigned leftX = 0;
unsigned leftY = 0;
unsigned rightX = num;
unsigned rightY = num;
while(leftX <= rightX)
{
b******n
发帖数: 823
45
来自主题: JobHunting版 - 1 11 21 1211 sequence的代码
写了个用string的
#include
#include
int main(int argc, char* argv[])
{
using namespace std;
cout << 1 << endl;
string s1, s2;
s1.push_back('1');
s1.push_back('1');
int loopcount = 0;
while(loopcount++ < 10)
{
cout << s1 << endl;
s1.push_back('a');
int i;
for (i=0; i {
int dupsize = 1;
while(s1[i] == s1[i+1])
{
dupsize++;
i++
f****4
发帖数: 1359
46
来自主题: JobHunting版 - 请教operator const char*() 的问题
conversion Operator :)
// code explains itself
class Test{
public:
operator const char*(){cout<<"Test"< };
void f(const char*)
{
cout<<"f()"< }
int main(){
Test t;
f(t);
return 0;
}
p**********s
发帖数: 115
47
来自主题: JobHunting版 - 回馈本版,贴ms onsite面经
我来解typedef那题:
#include
using namespace std;
void staticFun(int num) {
cout << "static function" << endl;
}
class foo{
public:
void classFun(int num) {
cout << "function in a class" << endl;
}
};
typedef void (*myStaticFun) (int);
typedef void (foo::*myClassFun) (int);
int main()
{
myStaticFun testStaticFun = staticFun;
testStaticFun(6);
foo foo1;
myClassFun testClassFun = &foo::classFun;
(foo1.*testClassFun)(6);
return 0;
}
P*******b
发帖数: 1001
48
来自主题: JobHunting版 - C++ object size一问
class Grandpa {
public:
Grandpa() { cout << "Grandpa " << endl; }
virtual ~Grandpa() {}
};
class Ma : public virtual Grandpa {
public:
Ma() { cout << "Ma" << endl; }
virtual ~Ma() {}
virtual void print() {}
};
为啥这里Ma的object size 等于grandpa的object size?
b********e
发帖数: 693
49
来自主题: JobHunting版 - one C++ question
what will happen if free an object in stack?
I did a test
A.
Class A{
public:
~A(){cout << "Destructor" << endl:}
}
int test()
{
A a;
free(&a);
return 0;
}
after the program run, it seems good, and print out Destructor
but after I insert one line into the func, the pgram crash
int test()
{
A a
free(&a);
cout << "test " << endl;
return 0;
}
c**y
发帖数: 172
50
Something is wrong with "string::iterator p ..."
string accessAString(const string& s) {
string a;
cout << s.c_str() << endl;
for (string::iterator p = s.begin(); p != s.end(); p++) {
cout << *p ;
}
cout << endl;
return a;
}
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)