Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

c++...物件要怎麼動態連接...

0 views
Skip to first unread message

藍月楓鈴

unread,
Mar 24, 2002, 3:43:32 AM3/24/02
to
※ 引述《Maste...@bbs.sayya.org (掛站僵屍........)》之銘言:
> ※ 引述《jenho...@bbs.tku.edu.tw (要平平安安喔~)》之銘言:
> > 《 在 CIA...@bbs.mhit.edu.tw (熱線妳和我....) 的大作中提到: 》
> public:
> 試試
> f *arr[2];
> arr[0]=new f(3);
> arr[1]=new f(4);
> > 用array alloc 10 Object,每個Object都要被initialize的話
> > 就在Constructor指定初值啊f::f(int data = 10)
> 整理一下^_^
> #include<iostream>
> using namespace std;
> class f{
> public:
> f(int i);
> };
> f::f(int i)
> {
> cout<<"Value:"<<i<<endl;
> }
> void main()
> {
> f *arr[2];
> arr[0]=new f(3);
> arr[1]=new f(4);
> }
> 希望對你有幫助^_^

會提這種問題的,大概都是對程序的最佳化很計較的人。

上述也是解決的辦法之一,只不過每個元素都還是指標…

以下的方法會比較機車一點:

在 class 中宣告靜態成員變數
class f
{
static int tmp;
f::f() { … }

}

然後利用 f::tmp 的設定,去操作 f 建構子。

:p~~~~ 只不過有點失去物件導向的意義了…
--
[1;32m┌───── [33m◆ [37mKKCITY [33m◆ [32m─────┐ [34m┐┌┐┐┌┐┌─┐┌┬┐┌┬┐┐ ┌ [m
[1;32m│ [31m bbs.kkcity.com.tw [32m│ [37m├┘┐├┘┐│ │ │ └┬┘ [m
[1;32m└── [34m《 [0;37mFrom:61.221.249.171 [1;34m》 [32m──┘ [36m┘ ┘┘ ┘└─┘└┴┘ ┴ ┴ [m

0 new messages