A
Alex Vinokur
Hi,
I have a problem with invoking an instance before main() when using memory allocation for static member.
Is the program below valid?
===========================================
Windows 2000
CYGWIN_NT-5.0 1.3.22(0.78/3/2)
GNU gcc version 3.2 20020927 (prerelease)
===========================================
// ====== C++ code : File t.cpp : BEGIN ======
#include <map>
#include <iostream>
using namespace std;
class Foo1 {};
template <typename T>
class Foo2
{
public :
static map<char, Foo1*> map1_s;
};
class Init
{
public :
Init();
};
// -------------------------
// Is such memory allocation legal ? :
template <typename T>
map<char, Foo1*> Foo2<T>::map1_s;
// -------------------------
// -------------------------
// Must we allocate memory as folowing ? :
// map<char, Foo1*> Foo2<int>::map1_s;
// map<char, Foo1*> Foo2<string>::map1_s;
// .........................
// -------------------------
Init::Init ()
{
cout << "Init ---> Start" << endl;
Foo2<int>::map1_s['A'] = new Foo1 ();
cout << "Init ---> Finish" << endl;
}
// ----------------------------
Init init;
int main(int argc, char *argv[])
{
cout << "===> Main" << endl;
return 0;
}
// ====== C++ code : File t.cpp : END ========
// ====== Compilation & Run : BEGIN ========
$ g++ t.cpp
$ a
Init ---> Start
Segmentation fault (core dumped)
// ====== Compilation & Run : END ==========
--
==========================================
Alex Vinokur
mailto:[email protected]
http://sourceforge.net/users/alexvn
http://www.simtel.net/search.php?action=search&authorName=Alex+Vinokur
==========================================
I have a problem with invoking an instance before main() when using memory allocation for static member.
Is the program below valid?
===========================================
Windows 2000
CYGWIN_NT-5.0 1.3.22(0.78/3/2)
GNU gcc version 3.2 20020927 (prerelease)
===========================================
// ====== C++ code : File t.cpp : BEGIN ======
#include <map>
#include <iostream>
using namespace std;
class Foo1 {};
template <typename T>
class Foo2
{
public :
static map<char, Foo1*> map1_s;
};
class Init
{
public :
Init();
};
// -------------------------
// Is such memory allocation legal ? :
template <typename T>
map<char, Foo1*> Foo2<T>::map1_s;
// -------------------------
// -------------------------
// Must we allocate memory as folowing ? :
// map<char, Foo1*> Foo2<int>::map1_s;
// map<char, Foo1*> Foo2<string>::map1_s;
// .........................
// -------------------------
Init::Init ()
{
cout << "Init ---> Start" << endl;
Foo2<int>::map1_s['A'] = new Foo1 ();
cout << "Init ---> Finish" << endl;
}
// ----------------------------
Init init;
int main(int argc, char *argv[])
{
cout << "===> Main" << endl;
return 0;
}
// ====== C++ code : File t.cpp : END ========
// ====== Compilation & Run : BEGIN ========
$ g++ t.cpp
$ a
Init ---> Start
Segmentation fault (core dumped)
// ====== Compilation & Run : END ==========
--
==========================================
Alex Vinokur
mailto:[email protected]
http://sourceforge.net/users/alexvn
http://www.simtel.net/search.php?action=search&authorName=Alex+Vinokur
==========================================