Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 382
  • Last Modified:

Template Overload

Hi, I am looking for some way for my class when passed as an object to return an address of type template T.
I want this so that I am able to pass my class to functions instead of what I currently need to do which is an
overloaded addressof operator and having to include & before the class.  How can I manage this?
0
List244
Asked:
List244
  • 6
  • 4
1 Solution
 
jkrCommented:
What are you meaning with "to return an address of type template T"?

Are you thinking of an

template<typename T>
class foo {

public:

operator T* () const { return (T*) this;}

// ...
};

?
0
 
jkrCommented:
Oops, example:

template<typename T>
class foo {

public:

operator T* () const { return (T*) this;}

// ...
};

void main () {

foo<int> f;
int* p = f;

}
0
 
List244Author Commented:
Something like this:

#include <iostream>
using namespace std;

template <class T>
class Class1
{
public:
      class Class2
      {
      public:
            Class2()
            {
                  SVal = new T;
                  *SVal = 5;
            }
            T* operator++()
            {
                  (*SVal)++;
                  return SVal;
            }
            T* operator&()
            {
                  return SVal;
            }
     protected:
          T* SVal;
     };
};
void foo(int* Temp)
{
      cout << Temp;
}
int main ()
{
      Class1<int>::Class2 C2;
      foo(&C2); //I would like to be able to do this with simply using:
      //foo(C2);
      return 0;
}
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
jkrCommented:
Then I guessed correctly about the purpose, works the same way:

#include <iostream>
using namespace std;

template <class T>
class Class1
{
public:
     class Class2
     {
     public:
          Class2()
          {
               SVal = new T;
               *SVal = 5;
          }
          T* operator++()
          {
               (*SVal)++;
               return SVal;
          }
          T* operator&()
          {
               return SVal;
          }
          operator T* () const { return (T*) this;} // conversion operator
     protected:
          T* SVal;
     };
};
void foo(int* Temp)
{
     cout << Temp;
}
int main ()
{
     Class1<int>::Class2 C2;
     foo(C2); // conversion operator will be called implicitly
     return 0;
}

(tested it also :o)
0
 
List244Author Commented:
That works great, thanks.
0
 
List244Author Commented:
0
 
jkrCommented:
Nope - at home right now, so my choice of compilers is quite limited...
0
 
jkrCommented:
Just as a side note - the above will work because 'SVal' is the only data member of the class  - to be sure, make that

          operator T* () const { return (T*) SVal;} // conversion operator
0
 
List244Author Commented:
Yeah, I got that from it, thanks for checking back on me though.
0
 
jkrCommented:
*LOL*, it just was a *boring* evening at the local watering hole, so I also thought of that ;o)

The key is to just *not* communicate such thoughts to the people around you, they might think you're plain crazy *eg*
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now