template re-initialization question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zl2k

    #1

    template re-initialization question

    hi, all

    Suppose I have a template class A
    template<class Tclass A{
    T a;
    A(T &a){this->a = a;} //***
    }

    Then I declare something as following in a header file

    A<intabc;
    A<int*p;

    In somewhere in the cpp file, I'll need to re-initialize them, but not
    sure I am doing it correctly.

    p = new A<int>(123); //looks fine?
    How can I re-initialize abc?

    If I'll need other type (double, short, etc) in the runtime, I have to
    declare A<double>, A<shortfor each of them in the header file? That
    looks like redundant. Thanks for comments.

    BTW, in ***, if I write in that way, will the instance of A keeps a
    "hard copy" of a such that A keeps what every transfered to a during
    initialization from outside even after the outside object is deleted
    afterwards? Or maybe I should write A(T a){this->a = a;}?
    zl2k

  • Victor Bazarov

    #2
    Re: template re-initialization question

    zl2k wrote:
    Suppose I have a template class A
    template<class Tclass A{
    T a;
    A(T &a){this->a = a;} //***
    Why not initialise it? Always prefer initialisation over assignment!
    }
    ^^^
    Semicolon is missing here
    >
    Then I declare something as following in a header file
    >
    A<intabc;
    A<int*p;
    If those are not inside a class, they are also _definitions_, since they
    do not have "extern".
    >
    In somewhere in the cpp file, I'll need to re-initialize them, but not
    sure I am doing it correctly.
    >
    p = new A<int>(123); //looks fine?
    That's not "initialise ", that's _assign_, again. And it has to be in
    a function, it cannot be outside of any function.
    How can I re-initialize abc?
    What do you mean? You already defined (and initialised) them.
    >
    If I'll need other type (double, short, etc) in the runtime, I have to
    declare A<double>, A<shortfor each of them in the header file? That
    looks like redundant. Thanks for comments.
    Why do you think you need to declare them in the first place? Only
    declare/define/initialise objects *right before you need them*.
    >
    BTW, in ***, if I write in that way, will the instance of A keeps a
    "hard copy" of a such that A keeps what every transfered to a during
    initialization from outside even after the outside object is deleted
    afterwards? Or maybe I should write A(T a){this->a = a;}?
    No, a reference should be fine, and even better is to use a reference
    to const 'T'.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • zl2k

      #3
      Re: template re-initialization question


      Victor Bazarov wrote:
      zl2k wrote:
      Suppose I have a template class A
      template<class Tclass A{
      T a;
      A(T &a){this->a = a;} //***
      >
      Why not initialise it? Always prefer initialisation over assignment!
      >
      }
      ^^^
      Semicolon is missing here
      >

      Then I declare something as following in a header file

      A<intabc;
      A<int*p;
      >
      If those are not inside a class, they are also _definitions_, since they
      do not have "extern".
      >

      In somewhere in the cpp file, I'll need to re-initialize them, but not
      sure I am doing it correctly.

      p = new A<int>(123); //looks fine?
      >
      That's not "initialise ", that's _assign_, again. And it has to be in
      a function, it cannot be outside of any function.
      >
      How can I re-initialize abc?
      >
      What do you mean? You already defined (and initialised) them.
      I mean how to assign another value. A pointer can do by new, I am not
      sure how abc can assigned to another value.
      >

      If I'll need other type (double, short, etc) in the runtime, I have to
      declare A<double>, A<shortfor each of them in the header file? That
      looks like redundant. Thanks for comments.
      >
      Why do you think you need to declare them in the first place? Only
      declare/define/initialise objects *right before you need them*.
      The problem is, if I declare/define/initialise objects *right before
      you need them* in somewhere, then in some other functions I may not be
      able to use it. I declare it in the header so that the program can use
      it everywhere, with different types defined at run time. Is it
      possible?

      >

      BTW, in ***, if I write in that way, will the instance of A keeps a
      "hard copy" of a such that A keeps what every transfered to a during
      initialization from outside even after the outside object is deleted
      afterwards? Or maybe I should write A(T a){this->a = a;}?
      >
      No, a reference should be fine, and even better is to use a reference
      to const 'T'.
      >
      V
      --
      Please remove capital 'A's when replying by e-mail
      I do not respond to top-posted replies, please don't ask

      Comment

      • Victor Bazarov

        #4
        Re: template re-initialization question

        zl2k wrote:
        [..]
        The problem is, if I declare/define/initialise objects *right before
        you need them* in somewhere, then in some other functions I may not be
        able to use it. I declare it in the header so that the program can use
        it everywhere, with different types defined at run time. Is it
        possible?
        What C++ book are you reading? You seem to misunderstand some fundamental
        concepts of C++. A variable has its type. A variable cannot change its
        type during run-time. It can only change its value.

        V
        --
        Please remove capital 'A's when replying by e-mail
        I do not respond to top-posted replies, please don't ask


        Comment

        Working...