• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Different classes instantiated from templates SHARE static members!!!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Different classes instantiated from templates SHARE static members!!!


  • Subject: Re: Different classes instantiated from templates SHARE static members!!!
  • From: Adin Hunter Baber <email@hidden>
  • Date: Sun, 7 Jan 2007 17:04:54 -0600


You could always move the static variable to inside the get function:

#include <Carbon/Carbon.h>
#include <iostream>

template<typename T>
class t
    {
    public:
        const char* getFoo();
    };

template<typename T>
const char* t<T>::getFoo()
    {
    static char *foo = "default";
    return foo;
    }

// specializations
typedef t<char> c1;
template<>
const char* t<char>::getFoo()
    {
    static char *foo = "foo1";
    return foo;
    }

typedef t<float> c2;
template<>
const char* t<float>::getFoo()
    {
    static char *foo = "foo2";
    return foo;
    }



int main (int argc, char * const argv[])
    {
    c1 obj1; c2 obj2;
    std::cout << obj1.getFoo() << " - " << obj2.getFoo() << std::endl;
    return 0;
    }


Side note: std::endl flushes the buffer.  Calling flush afterwards is redundant.

Adin Hunter Baber
email@hidden

"The more I learn, the more I learn how little I know."
    -- Socrates


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Different classes instantiated from templates SHARE static members!!!
      • From: Scott Ribe <email@hidden>
References: 
 >Re: Different classes instantiated from templates SHARE static members!!! (From: Scott Ribe <email@hidden>)

  • Prev by Date: Re: C Function declaration & fold
  • Next by Date: How do I detect that a button has been pushed and do something after it's been pushed?
  • Previous by thread: Re: Different classes instantiated from templates SHARE static members!!!
  • Next by thread: Re: Different classes instantiated from templates SHARE static members!!!
  • Index(es):
    • Date
    • Thread