Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 155097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:05:43+00:00 2026-05-11T10:05:43+00:00

I’d like to know some best practice when designing c++ classes. To put it

  • 0

I’d like to know some best practice when designing c++ classes.

To put it in context, I have a c++ class named Vec3.

class Vec3{ private:     float elements[3]; public:     Vec3(Vec3 v1){...}     Vec3(int x, int y, int z){...}     Vec3 add(Vec3 v1){...}     Vec3 add(int x, int y, int z){...}     ...     Vec3 multiply(Vec3 v1){...}     ...     int dotProduct(Vec3 v1){...}     Vec3 normalize(){...}     ....     int operator[](int pos){...} }; 

So, I have this class that does computing over a Vector of size 3. I’d like to know what’s better. Working with pointers or not.

Should I return pointer and have my parameters as Pointers or not.

Vec3 add(Vec3 v1) or Vec3* add(Vec3 v1) or Vec3* add(Vec3* v1) or ….

Now I’m confused, I don’t know if I should use pointer or not in my class. I guess there is always a way to send my arguments to function that don’t handle pointers…

Vec3* v2 = new Vec3(1,1,1); Vec3 sum = v1.add(*v2); 

And there is a solution that is probably the best of all I can come up with.. having both functions

Vec3 add(Vec3 v2){...} Vec3* add(Vec3* v2){...} 

But I fear this will lead to duplicate code and may be overhead.

Thank you for answers…btw, I could use a template to change the size of the Vector but I prefer to keep my Vec3 class alone and create a Vec4 class or name it Quaternion.

EDIT Here is the solution I came with. Feel free to comment or modify or reuse the code. One thing. I just want to mention that, in my case, This class is supposed to be transparent. Just like we add numbers.

int i = 10; int j = 15; int k = i + k; 

If the add overload modify the object that is calling the function in this case i. I would endup with a k being a reference to i and i being equal to 25. But what we really want here is a k equal to 25 and i,k unchanged.

Thats how my class work. Vec3 k = i + k will not modify i or k because we are creating a new number from these values. The only case where I return a reference is for +=, -=, ++, –…, set([XYZ])? and normalize.

It could be fun to do something like myvec.setX(10).normalize().scale(10)

NOTE: scale should return a reference. I didn’t see it but I guess it should be better this way.

Vec3 t = myvec.normalize().scale(100).copy(); 

http://pastebin.com/f413b7ffb

Thank you all, I’ll be working on the Matrix class now.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:05:44+00:00Added an answer on May 11, 2026 at 10:05 am

    These are the rules I usually stick to. Note ‘usually’, sometimes there are reasons for doing things differently…

    For parameters I don’t intend to modify I pass by value if they aren’t too large since they will be copied. If they are a bit large or aren’t copyable, you could use a const reference or a pointer (I prefer const reference).

    For parameters I do intend to modify, I use a reference.

    For return values I will return a copy whenever possible. Some times it’s handy to return a reference (this works well for a single function for get/set where you don’t need to do any special processing when the item is fetched or set).

    Where pointers really shine in my opinion is for instance variables where I want control over when it is constructed or destructed.

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Well, I'd do it like this: Make your table for… May 11, 2026 at 11:43 pm
  • Editorial Team
    Editorial Team added an answer Are you sure you are using the correct QMAKESPEC? I… May 11, 2026 at 11:43 pm
  • Editorial Team
    Editorial Team added an answer Google has the best tools for webmasters, but remember that… May 11, 2026 at 11:43 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.