Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > static std::vector<std::string> member and a static function

Reply
Thread Tools

static std::vector<std::string> member and a static function

 
 
A L
Guest
Posts: n/a
 
      06-15-2010
On Jun 15, 5:11*pm, Paul Bibbings <paul.bibbi...@gmail.com> wrote:
> Are you wanting to say that something like the following
> doesn't work for you with your version of VC 2008?


> * *d:\CPPProjects\CLCPP>cat static_member.cpp
> * *// file: static_member.cpp
>
> * *#include <vector>
> * *#include <string>
>
> * *class MyClass {
> * *public:
> * * * std::vector<std::string>& myFunction();
> * *private:
> * * * static std::vector<std::string> myStaticMember_;
> * *};
>
> * *std::vector<std::string> MyClass::myStaticMember_;
>
> * *std::vector<std::string>& MyClass::myFunction()
> * *{
> * * * return myStaticMember_;
> * *}
>
> * *int main()
> * *{
> * * * MyClass myClass;
> * * * std::vector<std::string>& vec_ref = myClass.myFunction();
> * *}



EXACTLY, Paul. It does not work even if I put the static vector of
strings in a static function and return a reference to it (instead of
putting it as a private static data member, that is). Do you have any
idea as to what this is all about? I am really drained on this - have
tried everything I could to solve it but there seems to be no
solution. I am surprised that your code works even if you put the
static vector of strings in the private section of your class and
access it through a static function! I know that is valid but
previously this was not working. It works only if I put the static
vector definition inside a static member function and return a
reference to it from the member function.
 
Reply With Quote
 
 
 
 
Paul Bibbings
Guest
Posts: n/a
 
      06-15-2010
A L <> writes:

> On Jun 15, 5:11*pm, Paul Bibbings <paul.bibbi...@gmail.com> wrote:
>> Are you wanting to say that something like the following
>> doesn't work for you with your version of VC 2008?

>
>> d:\CPPProjects\CLCPP>cat static_member.cpp
>> // file: static_member.cpp
>>
>> #include <vector>
>> #include <string>
>>
>> class MyClass {
>> public:
>> std::vector<std::string>& myFunction();
>> private:
>> static std::vector<std::string> myStaticMember_;
>> };
>>
>> std::vector<std::string> MyClass::myStaticMember_;
>>
>> std::vector<std::string>& MyClass::myFunction()
>> {
>> return myStaticMember_;
>> }
>>
>> int main()
>> {
>> MyClass myClass;
>> std::vector<std::string>& vec_ref = myClass.myFunction();
>> }

>
>
> EXACTLY, Paul. It does not work even if I put the static vector of
> strings in a static function and return a reference to it (instead of
> putting it as a private static data member, that is). Do you have any
> idea as to what this is all about? I am really drained on this - have
> tried everything I could to solve it but there seems to be no
> solution. I am surprised that your code works even if you put the
> static vector of strings in the private section of your class and
> access it through a static function! I know that is valid but
> previously this was not working. It works only if I put the static
> vector definition inside a static member function and return a
> reference to it from the member function.


If you can say that you are able to *copy & paste* the above code into a
new file in a new project and then attempt to build it and it fails,
then I would suggest that you have a problem with your installation
(rather than a bug). Do you have the VC command prompt? If so, copy
and paste the above code into notepad, save it as test_static.cpp, and
then invoke:

cl /EHsc test_static.cpp

from the VC command prompt. Report back on what the error output is, if
any.

Also, and this is important to know, your "It works only if I put the
static vector definition inside a static member function..." is a
red-herring. Inside a function definition, static does not have the
same meaning. It defines a vector of string that is *local to* the
function and, even if it has the same name as that of your declared
class member, it does *not* define that member.

Regards

Paul Bibbings
 
Reply With Quote
 
 
 
 
A L
Guest
Posts: n/a
 
      06-16-2010
Hi Paul,

On Jun 15, 6:04*pm, Paul Bibbings <paul.bibbi...@gmail.com> wrote:
> Also, and this is important to know, your "It works only if I put the
> static vector definition inside a static member function..." is a
> red-herring.



Paul, installing the Service Pack 1 has solved this problem. The code
works both ways now. By the way, Visual Studio Express edition
includes SP1 by default - you can check it out in its about box.
Thanks for your interest.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
What is the correct grammar to make a function call by using static member data which is a pointer to a ordinary class member function? zaeminkr@gmail.com C++ 3 07-06-2007 12:50 PM
How do you call a regular member function from a static member function? aling C++ 6 10-30-2005 04:38 AM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM
performance of static member function vs. instance member function 0to60 C++ 4 11-21-2003 05:25 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57