Solved

How to expose vector members

Posted on 2002-04-25
11
289 Views
Last Modified: 2013-12-14
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
Comment
Question by:MakeItWork614
  • 6
  • 2
  • 2
  • +1
11 Comments
 
LVL 4

Expert Comment

by:IainHere
ID: 6968811
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
 
LVL 2

Expert Comment

by:LoungeLizard
ID: 6969967
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
 

Author Comment

by:MakeItWork614
ID: 6970176
>>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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 2

Expert Comment

by:LoungeLizard
ID: 6970194
In that case just use

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

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

0
 

Author Comment

by:MakeItWork614
ID: 6970199
LoungeLizard,
I tried it that way, but it doesn't compile.
VC gives a compile error.
0
 

Author Comment

by:MakeItWork614
ID: 6970203
I get an error stating that begin is not a member of base class widget<T>
0
 
LVL 30

Expert Comment

by:Axter
ID: 6970208
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
 

Author Comment

by:MakeItWork614
ID: 6970214
Axter,
That method compiles, but it still brings the std::vector into the global namespace.
0
 
LVL 30

Accepted Solution

by:
Axter earned 100 total points
ID: 6970225
>>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
 

Author Comment

by:MakeItWork614
ID: 6970241
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
 

Author Comment

by:MakeItWork614
ID: 6970243
Axter,
If no one comes up with something better, I'll award you the points.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Reference external folders in Visual Studio 4 81
Compile GLUT with Visual Studio 2015 1 143
convert char array to number in c 5 85
c++, dynamic object by json 1 41
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now