Pointers question

This is a discussion on Pointers question within the C++ Programming forums, part of the General Programming Boards category; I am trying to write use pointers for some function arguments. The reason I am using pointers and not references ...

  1. #1
    JMK
    JMK is offline
    Registered User
    Join Date
    May 2010
    Posts
    24

    Pointers question

    I am trying to write use pointers for some function arguments. The reason I am using pointers and not references is that either the src or dst arguments might be NULL.
    Code:
    Vec3 TFormPoint(const Vec3& point, Entity* src, Entity* dst)
    {
    	Mat4* srcmat;
    	Mat4* dstmat;
    	if (src!=0) {
    		srcmat = src.mat;
    	}
    	if (dst!=0) {
    		dstmat = dst.mat;
    	}
    	//do some more stuff
    }
    The entity class looks like this:
    Code:
    class Entity
    {
    	public:
    	Entity();
    	Mat4 mat;
    }
    Why is the compiler complaining that "left of '.mat' must have class/struct/union"? How do I convert the reference to a pointer? What should this code look like?

  2. #2
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Because src and dst are pointers, as you have written. Thus what you are looking to do is:
    Code:
    srcmat = src->mat;
    dstmat = dst->mat;
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  3. #3
    JMK
    JMK is offline
    Registered User
    Join Date
    May 2010
    Posts
    24
    Looks like now it needs to convert the mat reference to a pointer?:
    Code:
    Vec3 TFormPoint(const Vec3& point, Entity* src, Entity* dst)
    {
    	Mat4* srcmat;
    	Mat4* dstmat;
    	if (src!=0) {
    		srcmat = src->mat;
    	}
    	if (dst!=0) {
    		dstmat = dst->mat;
    	}
    	//Do stuff
    }
    1>c:\projects\leadwerks 3.0\engine\transform.cpp(21) : error C2440: '=' : cannot convert from 'DE2::Mat4' to 'DE2::Mat4 *'
    Error occurs on this line:
    Code:
    srcmat = src->mat;
    Last edited by JMK; 05-29-2010 at 12:07 PM.

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Well I was just guessing since you did not post the code to the Entity class; if mat is not a pointer then:
    Code:
    srcmat = &(*src).mat;
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    JMK
    JMK is offline
    Registered User
    Join Date
    May 2010
    Posts
    24
    Thanks, it seems to be working.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    Or simply:
    Code:
    srcmat = &src->mat;
    Be careful with the case where it is null though. At the moment srcmat will simply be uninitialised!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    JMK
    JMK is offline
    Registered User
    Join Date
    May 2010
    Posts
    24
    Yep:
    Code:
    if (src!=NULL)
    {
    srcmat = &src->mat;
    }
    I ended up making a few overloaded functions so that both references and pointers can be used. References would be adequate if they could be set to null, but they can't.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array pointers question?
    By ben2000 in forum C Programming
    Replies: 4
    Last Post: 07-26-2007, 01:31 AM
  2. A question on Pointers & Structs
    By FJ8II in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2007, 10:56 PM
  3. simple pointers question
    By euphie in forum C Programming
    Replies: 4
    Last Post: 05-25-2006, 01:51 AM
  4. Very stupid question involving pointers
    By 7smurfs in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 05:15 PM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21