Accessing a variable using namespace and a template class





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I can't access the variable width.



The A.h file:



namespace AN{
template <typename T> class A{
public:
unsigned int width; #The variable
...
}
}


The B.cpp file:



#include "A.h"
using namespace AN;
namespace BN{
bool something(){
unsigned int * w = AN::&width;
}
}


I tried also AN::A::&width but it doesn't work as well.










share|improve this question

























  • AN::&width makes no sense. width is not a member of AN, but of AN::A<SomeType>. You need an instance of class A<SomeType>, then you can refer to the data member of that instance.

    – Igor Tandetnik
    Jan 3 at 16:06













  • width is a non-static member variable. So you need an instance of A in order to access it's variable. AN::A<int> a; unsigned int* w = &a.width;

    – john
    Jan 3 at 16:06













  • unsigned int * w = AN::A<unsigned int>::&width; .. I tried it but it is stays that width is undefined..

    – Giwrgos Kostopoulos
    Jan 3 at 16:08











  • if i make static would it work?

    – Giwrgos Kostopoulos
    Jan 3 at 16:09











  • @GiwrgosKostopoulos No, you need a variable of type A, AN::A<unsigned int> a; unsigned int * w = &a.width;. I think really need you need to learn some basic C++ concepts.

    – john
    Jan 3 at 16:09




















0















I can't access the variable width.



The A.h file:



namespace AN{
template <typename T> class A{
public:
unsigned int width; #The variable
...
}
}


The B.cpp file:



#include "A.h"
using namespace AN;
namespace BN{
bool something(){
unsigned int * w = AN::&width;
}
}


I tried also AN::A::&width but it doesn't work as well.










share|improve this question

























  • AN::&width makes no sense. width is not a member of AN, but of AN::A<SomeType>. You need an instance of class A<SomeType>, then you can refer to the data member of that instance.

    – Igor Tandetnik
    Jan 3 at 16:06













  • width is a non-static member variable. So you need an instance of A in order to access it's variable. AN::A<int> a; unsigned int* w = &a.width;

    – john
    Jan 3 at 16:06













  • unsigned int * w = AN::A<unsigned int>::&width; .. I tried it but it is stays that width is undefined..

    – Giwrgos Kostopoulos
    Jan 3 at 16:08











  • if i make static would it work?

    – Giwrgos Kostopoulos
    Jan 3 at 16:09











  • @GiwrgosKostopoulos No, you need a variable of type A, AN::A<unsigned int> a; unsigned int * w = &a.width;. I think really need you need to learn some basic C++ concepts.

    – john
    Jan 3 at 16:09
















0












0








0








I can't access the variable width.



The A.h file:



namespace AN{
template <typename T> class A{
public:
unsigned int width; #The variable
...
}
}


The B.cpp file:



#include "A.h"
using namespace AN;
namespace BN{
bool something(){
unsigned int * w = AN::&width;
}
}


I tried also AN::A::&width but it doesn't work as well.










share|improve this question
















I can't access the variable width.



The A.h file:



namespace AN{
template <typename T> class A{
public:
unsigned int width; #The variable
...
}
}


The B.cpp file:



#include "A.h"
using namespace AN;
namespace BN{
bool something(){
unsigned int * w = AN::&width;
}
}


I tried also AN::A::&width but it doesn't work as well.







c++ namespaces






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 16:16









François Andrieux

16.5k32950




16.5k32950










asked Jan 3 at 16:03









Giwrgos KostopoulosGiwrgos Kostopoulos

1




1













  • AN::&width makes no sense. width is not a member of AN, but of AN::A<SomeType>. You need an instance of class A<SomeType>, then you can refer to the data member of that instance.

    – Igor Tandetnik
    Jan 3 at 16:06













  • width is a non-static member variable. So you need an instance of A in order to access it's variable. AN::A<int> a; unsigned int* w = &a.width;

    – john
    Jan 3 at 16:06













  • unsigned int * w = AN::A<unsigned int>::&width; .. I tried it but it is stays that width is undefined..

    – Giwrgos Kostopoulos
    Jan 3 at 16:08











  • if i make static would it work?

    – Giwrgos Kostopoulos
    Jan 3 at 16:09











  • @GiwrgosKostopoulos No, you need a variable of type A, AN::A<unsigned int> a; unsigned int * w = &a.width;. I think really need you need to learn some basic C++ concepts.

    – john
    Jan 3 at 16:09





















  • AN::&width makes no sense. width is not a member of AN, but of AN::A<SomeType>. You need an instance of class A<SomeType>, then you can refer to the data member of that instance.

    – Igor Tandetnik
    Jan 3 at 16:06













  • width is a non-static member variable. So you need an instance of A in order to access it's variable. AN::A<int> a; unsigned int* w = &a.width;

    – john
    Jan 3 at 16:06













  • unsigned int * w = AN::A<unsigned int>::&width; .. I tried it but it is stays that width is undefined..

    – Giwrgos Kostopoulos
    Jan 3 at 16:08











  • if i make static would it work?

    – Giwrgos Kostopoulos
    Jan 3 at 16:09











  • @GiwrgosKostopoulos No, you need a variable of type A, AN::A<unsigned int> a; unsigned int * w = &a.width;. I think really need you need to learn some basic C++ concepts.

    – john
    Jan 3 at 16:09



















AN::&width makes no sense. width is not a member of AN, but of AN::A<SomeType>. You need an instance of class A<SomeType>, then you can refer to the data member of that instance.

– Igor Tandetnik
Jan 3 at 16:06







AN::&width makes no sense. width is not a member of AN, but of AN::A<SomeType>. You need an instance of class A<SomeType>, then you can refer to the data member of that instance.

– Igor Tandetnik
Jan 3 at 16:06















width is a non-static member variable. So you need an instance of A in order to access it's variable. AN::A<int> a; unsigned int* w = &a.width;

– john
Jan 3 at 16:06







width is a non-static member variable. So you need an instance of A in order to access it's variable. AN::A<int> a; unsigned int* w = &a.width;

– john
Jan 3 at 16:06















unsigned int * w = AN::A<unsigned int>::&width; .. I tried it but it is stays that width is undefined..

– Giwrgos Kostopoulos
Jan 3 at 16:08





unsigned int * w = AN::A<unsigned int>::&width; .. I tried it but it is stays that width is undefined..

– Giwrgos Kostopoulos
Jan 3 at 16:08













if i make static would it work?

– Giwrgos Kostopoulos
Jan 3 at 16:09





if i make static would it work?

– Giwrgos Kostopoulos
Jan 3 at 16:09













@GiwrgosKostopoulos No, you need a variable of type A, AN::A<unsigned int> a; unsigned int * w = &a.width;. I think really need you need to learn some basic C++ concepts.

– john
Jan 3 at 16:09







@GiwrgosKostopoulos No, you need a variable of type A, AN::A<unsigned int> a; unsigned int * w = &a.width;. I think really need you need to learn some basic C++ concepts.

– john
Jan 3 at 16:09














1 Answer
1






active

oldest

votes


















2














This has nothing to do with templates. It's about classes and objects. The address of width is determined by the object that it's a part of; without an object, there is no width.



However, without an object you can create a pointer-to-member; it's not an ordinary pointer (if it was, it would be called "pointer"). Like this:



class A {
public:
int width;
};

int A::*w = &A::width;


You use it to access that variable when you create an object:



A a;
a.*w = 3;
A aa;
aa.*w = 4;


If you really only want one value of width for every object of your type, yes, you can make it a static member:



class A {
public:
static int width;
};
int A::width;


Now you can create a pointer to that member as an ordinary pointer:



int* w = &A::width;


and you can use w as an ordinary pointer:



*w = 3;





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54025850%2faccessing-a-variable-using-namespace-and-a-template-class%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    This has nothing to do with templates. It's about classes and objects. The address of width is determined by the object that it's a part of; without an object, there is no width.



    However, without an object you can create a pointer-to-member; it's not an ordinary pointer (if it was, it would be called "pointer"). Like this:



    class A {
    public:
    int width;
    };

    int A::*w = &A::width;


    You use it to access that variable when you create an object:



    A a;
    a.*w = 3;
    A aa;
    aa.*w = 4;


    If you really only want one value of width for every object of your type, yes, you can make it a static member:



    class A {
    public:
    static int width;
    };
    int A::width;


    Now you can create a pointer to that member as an ordinary pointer:



    int* w = &A::width;


    and you can use w as an ordinary pointer:



    *w = 3;





    share|improve this answer




























      2














      This has nothing to do with templates. It's about classes and objects. The address of width is determined by the object that it's a part of; without an object, there is no width.



      However, without an object you can create a pointer-to-member; it's not an ordinary pointer (if it was, it would be called "pointer"). Like this:



      class A {
      public:
      int width;
      };

      int A::*w = &A::width;


      You use it to access that variable when you create an object:



      A a;
      a.*w = 3;
      A aa;
      aa.*w = 4;


      If you really only want one value of width for every object of your type, yes, you can make it a static member:



      class A {
      public:
      static int width;
      };
      int A::width;


      Now you can create a pointer to that member as an ordinary pointer:



      int* w = &A::width;


      and you can use w as an ordinary pointer:



      *w = 3;





      share|improve this answer


























        2












        2








        2







        This has nothing to do with templates. It's about classes and objects. The address of width is determined by the object that it's a part of; without an object, there is no width.



        However, without an object you can create a pointer-to-member; it's not an ordinary pointer (if it was, it would be called "pointer"). Like this:



        class A {
        public:
        int width;
        };

        int A::*w = &A::width;


        You use it to access that variable when you create an object:



        A a;
        a.*w = 3;
        A aa;
        aa.*w = 4;


        If you really only want one value of width for every object of your type, yes, you can make it a static member:



        class A {
        public:
        static int width;
        };
        int A::width;


        Now you can create a pointer to that member as an ordinary pointer:



        int* w = &A::width;


        and you can use w as an ordinary pointer:



        *w = 3;





        share|improve this answer













        This has nothing to do with templates. It's about classes and objects. The address of width is determined by the object that it's a part of; without an object, there is no width.



        However, without an object you can create a pointer-to-member; it's not an ordinary pointer (if it was, it would be called "pointer"). Like this:



        class A {
        public:
        int width;
        };

        int A::*w = &A::width;


        You use it to access that variable when you create an object:



        A a;
        a.*w = 3;
        A aa;
        aa.*w = 4;


        If you really only want one value of width for every object of your type, yes, you can make it a static member:



        class A {
        public:
        static int width;
        };
        int A::width;


        Now you can create a pointer to that member as an ordinary pointer:



        int* w = &A::width;


        and you can use w as an ordinary pointer:



        *w = 3;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 3 at 16:15









        Pete BeckerPete Becker

        59k442122




        59k442122
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54025850%2faccessing-a-variable-using-namespace-and-a-template-class%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            Npm cannot find a required file even through it is in the searched directory