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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:59:14+00:00 2026-05-15T02:59:14+00:00

That may sound a little confusing. Basically, I have a function CCard newCard() {

  • 0

That may sound a little confusing. Basically, I have a function

CCard newCard() 
{
    /* Used to store the string variables intermittantly */
    std::stringstream ssPIN, ssBN;
    int picker1, picker2;
    int pin, bankNum;

    /* Choose 5 random variables, store them in stream */
    for( int loop = 0; loop < 5; ++loop )
    {
        picker1 = rand() % 8 + 1;
        picker2 = rand() % 8 + 1;
        ssPIN << picker1;
        ssBN  << picker2;
    }
    /* Convert them */
    ssPIN >> pin;
    ssBN  >> bankNum;

    CCard card( pin, bankNum );
    return card;
}

that creates a new CCard variable and returns it to the caller

CCard card = newCard();

My teacher advised me that doing this is a violation of OOP principles and has to be put in the class. He told me to use this method as a constructor. Which I did:

CCard::CCard()
{
    m_Sperre   = false;
    m_Guthaben = rand() % 1000;

    /* Work */

    /* Convert them */
    ssPIN >> m_Geheimzahl;
    ssBN  >> m_Nummer;
}   

All variables with m_ are member variables. However, the constructor works when I initialize the card normally

CCard card();

at the start of the program. However, I also have a function, that is supposed to create a new card and return it to the user, this function is now broken.

The original command: card = newCard(); isn’t available anymore, and card = new CCard(); doesn’t work. What other options do I have? I have a feeling using the constructor won’t work, and that I probably should just create a class method newCard, but I want to see if it is somehow at all possible to do it the way the teacher wanted.

This is creating a lot of headaches for me. I told the teacher that this is a stupid idea and not everything has to be classed in OOP. He has since told me that Java or C# don’t allow code outside of classes, which sounds a little incredible. Not sure that you can do this in C++, especially when templated functions exist, or generic algorithms. Is it true that this would be bad code for OOP in C++ if I didn’t force it into a class?

EDIT:

I would like to thank everyone for their helpful answers! However, I believe that my phrasing of the question is a little screwed up, and I think people don’t understand what I am looking for.

I do not want to initialize another member of type CCard. I want to intitialize

CCard card once and then give card new values through the constructor, because this is what the teacher told me to do. I do not want to create a new CCard object, just use the same variable with new values over again.

This is why I said it probably won’t work with the constructor. So I have a function that is supposed to take the initialized variable card and then call the constructor again( “What?” is what I told the teacher ) and then give it new values.

Example code:

void foo()
{
    /* Initialize card with constructor */
    CCard card;
    /* Give it new values through the constructor AGAIN... */
    card;
}

This is the actual question. I’m sorry if I confused everybody xD

  • 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-05-15T02:59:14+00:00Added an answer on May 15, 2026 at 2:59 am

    Just some basic pointers, because you have some misunderstandings.

    This doesn’t intialize a CCard, object. It declares card to be a function returning a CCard and taking no parameters.

    CCard card();
    

    If you want to construct a CCard object then just do this.

    CCard card;
    

    Your constructor will be called and card should be initialized correctly.

    The expression new CCard() dynamically creates a CCard object and “returns” a pointer to a CCard object which you would then need to delete. I don’t recommend using this until you’ve successfully created and understood using local objects first.

    EDIT in response to question edit

    A constructor can only be called once in an object’s lifetime so you can’t ever ‘re-construct’ an object. If your class is assignable, though, you can typically assign it the value of a default constructed temporary:

    card = CCard();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question that may sound odd, but being somewhat of a newbie,
Ok, the title may sound a little weird, but I think that kinda describe
I have a question that for some with experience may sound stupid, but I
I have a question, that may sound strange. I am interested to know if
My question may sound a little confusing, but I hope after reading this it
Ok this may sound a little weird but what i have is a frameset
I know that may sound silly, but I'm trying to submit a form on
I have a project that may be started on local machine with ./manage.py runserver
I have a variable that may contain objects or may be undefined. I wish
We have some stuff that may be exported into various formats. Currently we have

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.