Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > typedef

Reply
Thread Tools

typedef

 
 
Gurikar
Guest
Posts: n/a
 
      04-27-2005
Does typedef vector<std::string> vNames; creates vector object?

 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      04-27-2005
Gurikar wrote:

> Does typedef vector<std::string> vNames; creates vector object?


It instantiates the vector template, creating a type called vNames, which
can generally be used as a class. No object yet.

vNames names; creates an object.

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand


 
Reply With Quote
 
 
 
 
Chris \( Val \)
Guest
Posts: n/a
 
      04-27-2005

"Gurikar" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
| Does typedef vector<std::string> vNames; creates vector object?

typedef' in themselves do not create any objects,
they create 'synonyms' - AKA: A symbolic substitute,
also known as an 'alias' for another object.

Objects are instantiated later down the track:

This:
typedef vector<std::string> vNames;

....introduces a 'synonym' called: 'vNames'

The following:
vNames vNamesObject;

....instantiates the actual object.

Cheers,
Chris Val




 
Reply With Quote
 
Gurikar
Guest
Posts: n/a
 
      04-27-2005
Thank you very much

 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      04-27-2005
Chris ( Val ) wrote:

> typedef' in themselves do not create any objects,
> they create 'synonyms' - AKA: A symbolic substitute,
> also known as an 'alias' for another object.
>
> Objects are instantiated later down the track:
>
> This:
> typedef vector<std::string> vNames;


In the special case of a template, typedef-ing also instantiates the target
class, meaning certain lookup paths from inside the template to external
identifiers get nailed down. By contrast...

#define vNames vector<std::string>

would reinstantiate the template each time vNames is used. I suspect that
can introduce subtle bugs.

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand



 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      04-27-2005
Chris ( Val ) wrote:

> | Does typedef vector<std::string> vNames; creates vector object?
>
> typedef' in themselves do not create any objects,
> they create 'synonyms' - AKA: A symbolic substitute,
> also known as an 'alias' for another object.


No. It creates an alias for another type, not for an object.

 
Reply With Quote
 
Chris \( Val \)
Guest
Posts: n/a
 
      04-28-2005

"Phlip" <(E-Mail Removed)> wrote in message
news:fDMbe.1058$(E-Mail Removed) ...
| Chris ( Val ) wrote:
|
| > typedef' in themselves do not create any objects,
| > they create 'synonyms' - AKA: A symbolic substitute,
| > also known as an 'alias' for another object.
| >
| > Objects are instantiated later down the track:
| >
| > This:
| > typedef vector<std::string> vNames;
|
| In the special case of a template, typedef-ing also instantiates the target
| class,

Are you sure about that ?

I have not heard of a typedef being responsible
for the actual instantiation of any object(s) ?

| meaning certain lookup paths from inside the template to external
| identifiers get nailed down. By contrast...

I'm not sure what you mean by that.
Can you please elaborate a little further ?

| #define vNames vector<std::string>
|
| would reinstantiate the template each time vNames is used.

Are you certain of this ?
Hmn..., maybe I'm misunderstanding what you mean ?

| I suspect that can introduce subtle bugs.

Ok, here are a couple of tests - Please feel free to
correct me where ever I have erred:

# include <iostream>
# include <ostream>
# include <vector>

namespace Std
{
template<class T> struct MyString
{
MyString() { std::cout << "Ctor-1 MyString\n"; }
MyString( T ) { std::cout << "Ctor-2 MyString\n"; }
MyString( const MyString& )
{ std::cout << "Copy-Ctor MyString\n"; }
};
}

int main()
{
#define vNames std::vector<Std::MyString<int> >
typedef std::vector<Std::MyString<int> > VecMyStr;

VecMyStr V1;
//V1.push_back( Std::MyString<int>() );
//V1.push_back( Std::MyString<int>( 5 ) );

vNames V2;
//V2.push_back( Std::MyString<int>() );
//V2.push_back( Std::MyString<int>( 10 ) );

return 0;
}

In both cases, no objects are constructed until the
'push_back()' member of std::vector is invoked. You
can also verify that the vector size is zero in each
test case.

Can you please explain via an example, exactly what
you were trying to convey if not the above ?

Cheers,
Chris Val


 
Reply With Quote
 
Chris \( Val \)
Guest
Posts: n/a
 
      04-28-2005

"Rolf Magnus" <(E-Mail Removed)> wrote in message news:d4o51j$7t1$05$(E-Mail Removed)-online.com...
| Chris ( Val ) wrote:
|
| > | Does typedef vector<std::string> vNames; creates vector object?
| >
| > typedef' in themselves do not create any objects,
| > they create 'synonyms' - AKA: A symbolic substitute,
| > also known as an 'alias' for another object.
|
| No. It creates an alias for another type, not for an object.

Correct.
To be even more pedantic:
7.13: A typedef-name is thus a synonym for another type.

Cheers,
Chris Val


 
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
CRTP-problem: How can the base class typedef a derived class' typedef? oor C++ 0 05-20-2008 12:39 PM
java needs typedef Steve Green Java 11 03-25-2005 09:52 AM
Typedef of a template? Richard van Wegen C++ 3 07-15-2003 07:22 AM
template typedef as return type Robert A. T. Kaldy C++ 1 07-09-2003 06:25 PM
typedef enum qazmlp C++ 2 07-02-2003 11:55 AM



Advertisments