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: 316
  • Last Modified:

How to expose vector members

I'm trying to expose certain vector members

template<class T>
class widget : private std::vector<T>
{
public:
using vector<T>::end; //This doesn't compile
vector<T>::begin;  //This also doesn't compile
};

Neither methods will compile.  My compiler is VC++ 6.0.
I don't know if it's a compiler problem, or a syntax problem.
0
MakeItWork614
Asked:
MakeItWork614
  • 6
  • 2
  • 2
  • +1
1 Solution
 
IainHereCommented:
using namespace std;

before the above and it will compile.  begin() and end() are public in the base class.  I don't think it is wise to change their functionality in a derived class.
0
 
LoungeLizardCommented:
Interesting ...

If I might ask as an aside:

1. Is there something in the ANSI C++ standard regarding the default namespace? In the GCC the above will compile without "using namespace std" i.e. it assumes "std" is the default namespace.

2. A derived private member can be made public in this way, but it seems that a derived public member cannot be made private. I assume that is because it can be accessed in the base class anyway, hence the compiler will ignore the request the make it private in the derived class. Any thoughts?
0
 
MakeItWork614Author Commented:
>>using namespace std;
In my original code, I use std::vector for the inheritence.

I don't want to use a blanket using statement, because this template is going in my header file, which would bring the std namespace into the global namespace for any file using the header.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LoungeLizardCommented:
In that case just use

template<class T> class widget : private std::vector<T>
{
 public:

   std::vector<T>::begin;
   std::vector<T>::end;
};

0
 
MakeItWork614Author Commented:
LoungeLizard,
I tried it that way, but it doesn't compile.
VC gives a compile error.
0
 
MakeItWork614Author Commented:
I get an error stating that begin is not a member of base class widget<T>
0
 
AxterCommented:
You can try using the following method.

namespace
{
     using std::vector;
     template<class T>
          class widget : private vector<T>
     {
public:
     using vector<T>::end;
     vector<T>::begin;
     };
}

The error you're getting with VC is a VC specific compiler problem.  The original code should have been able to compile.
0
 
MakeItWork614Author Commented:
Axter,
That method compiles, but it still brings the std::vector into the global namespace.
0
 
AxterCommented:
>>That method compiles, but it still brings the
>>std::vector into the global namespace.

The only way around this in VC (6.0) is to put it in a specific namespace.
Example:
namespace xyz
{
    using std::vector;
    template<class T>
         class widget : private vector<T>
    {
public:
    using vector<T>::end;
    vector<T>::begin;
    };
}

0
 
MakeItWork614Author Commented:
Axter,
That works, but I like to keep this question open to see if someone has a better solution that will work with VC.

Thanks
0
 
MakeItWork614Author Commented:
Axter,
If no one comes up with something better, I'll award you the points.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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