typedef

Discussion in 'C++' started by Gurikar, Apr 27, 2005.

  1. Gurikar

    Gurikar Guest

    Does typedef vector<std::string> vNames; creates vector object?
     
    Gurikar, Apr 27, 2005
    #1
    1. Advertising

  2. Gurikar

    Phlip Guest

    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
     
    Phlip, Apr 27, 2005
    #2
    1. Advertising

  3. "Gurikar" <> wrote in message
    news:...
    | 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
     
    Chris \( Val \), Apr 27, 2005
    #3
  4. Gurikar

    Gurikar Guest

    Thank you very much
     
    Gurikar, Apr 27, 2005
    #4
  5. Gurikar

    Phlip Guest

    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
     
    Phlip, Apr 27, 2005
    #5
  6. Gurikar

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, Apr 27, 2005
    #6
  7. "Phlip" <> wrote in message
    news:fDMbe.1058$...
    | 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
     
    Chris \( Val \), Apr 28, 2005
    #7
  8. "Rolf Magnus" <> wrote in message news:d4o51j$7t1$05$-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
     
    Chris \( Val \), Apr 28, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Green

    java needs typedef

    Steve Green, Mar 24, 2005, in forum: Java
    Replies:
    11
    Views:
    11,392
    Joona I Palaste
    Mar 25, 2005
  2. qazmlp

    typedef enum

    qazmlp, Jul 2, 2003, in forum: C++
    Replies:
    2
    Views:
    458
    Alexander Terekhov
    Jul 2, 2003
  3. Robert A. T. Kaldy

    template typedef as return type

    Robert A. T. Kaldy, Jul 9, 2003, in forum: C++
    Replies:
    1
    Views:
    417
    Michael Kochetkov
    Jul 9, 2003
  4. Richard van Wegen

    Typedef of a template?

    Richard van Wegen, Jul 14, 2003, in forum: C++
    Replies:
    3
    Views:
    449
    Richard van Wegen
    Jul 15, 2003
  5. oor
    Replies:
    0
    Views:
    1,357
Loading...

Share This Page