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 7832577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:17:37+00:00 2026-06-02T12:17:37+00:00

When it comes to creating classes I’ve been told NO public data members. That’s

  • 0

When it comes to creating classes I’ve been told NO public data members. That’s fine and I understand why but here’s my issue. I’ve got a struct that I am using in a class (Linear linked list). I’d really like to initialize one of the struct fields (an array) to NULL so that I can check that the client is passing me valid structs, not uninitalized one’s that are full of garbage values. I realize c++ won’t let me set a default value in the struct declaration, so how do I protect from the client giving me garbage?

struct task {
    char *description;
    char *name;
    float time_remaining;
    float time_spent;
};

Then in my class I’m trying to do something like:

//I am making a copy of "to_add" and storing it in the LLL
int list::add_to_list(const task & to_add)
{ /.../ }

I don’t want the user adding a bunch of uninitialized “task’s” to the linked list… What to do? When I turned that struct into a class and moved the data members to private I had a TON of issues trying to access the data members to make copies of them. I was very careful not to do anything to effect the value of the var’s but I couldn’t get away from the compiler giving me errors about “error: passing ‘const task’ as ‘this’ argument of ‘char* task::get_comp_name()’ discards qualifiers [-fpermissive]” (“get_comp_name”) was one of the getter’s that I was sure wasn’t editing any values, just passing me a copy) Please help me before I shoot myself. In the face.

  • 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. Editorial Team
    Editorial Team
    2026-06-02T12:17:38+00:00Added an answer on June 2, 2026 at 12:17 pm

    In C++ a struct and a class are the same except for access control. So the struct’s default access to members and inheritance is public, whereas the class’ one is private. So you can give your struct a defult constructor and others to initialize it.

    struct task {
        task() : desctiption(0), name(0), time_remaining(0.), time_spent(0.) {}
        char *description;
        char *name;
        float time_remaining;
        float time_spent;
    };
    

    One side-effect of adding a constructor is that the struct isn’t an aggregate anymore. This may or may not be an issue for you.

    In C++11, you are also able to initialize members at the point of declaration:

    struct task {
        char *description{nullptr};
        char *name{nullptr};
        float time_remaining{0};
        float time_spent{0};
    };
    

    This in-place initialization accepts type x = y syntax too, and argument-less {} initialization results in value initialization, which results in zero initialization for primitive types, so the arguments in the example could have been omitted.

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

Sidebar

Related Questions

Issue comes up when i try to unload plugin that is loaded and load
This scenario comes up often, now that I use LinkToSql and the classes it
I've been working on a framework in AS3 that I want to release, but
I have an issue with a Windows Forms application that I am creating. The
It seems to me that non-public top-level classes and static nested classes essentially perform
When it comes to creating stored procedures, views, functions, etc., is it better to
I'm creating a v8 shell based console, I took the sample code that cames
Data comes in by email as a zipped file. The Java solution we wrote
Android comes with lots of system resources ( android.R ) that can be used
Here comes a simple question, for all those who know javascript. I made an

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.