0
#include<iostream>

using namespace std; 
class B; 
class A {   public:
      void UseClassB(B& test);

}; 
class B{

    int a;
    friend void A::UseClassB(B& test);
    void privateSeta(int x){a=x;};

    public:
        void publicSeta(int x){a=x;};

}; 
void A::UseClassB(B& test){
   cout<<test.a<<endl; } 

int main() {    A a;    B b;
       b.publicSeta(5);    
       B b2;    
       //b2.privateSeta(6) ; //....(*)
       a.UseClassB(b);
        
    return 0; }

1 Without using any setter method as in the code, just using the default compiler-generated constructor, how can I instantiate an object with an specific value of member a, say a=10. Is it possible or I am limited to instantiate one with some junk value?

2 Supposing I don't have publicSeta, how can I use privateSeta in the main function to actually set the value of member a? Is it even possible?

some_math_guy
  • 333
  • 1
  • 8
  • Does https://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor?rq=1 help? – Werner Henze Feb 28 '21 at 16:51
  • @WernerHenze to use a member initialization list I need to define the constructor myself. I am asking if it is possible to do it with no explicitly-defined constructor – some_math_guy Feb 28 '21 at 16:56
  • 1
    Since C++11 you can use an in-class initializer. – Werner Henze Feb 28 '21 at 17:00
  • @Ron In the main function – some_math_guy Feb 28 '21 at 17:01
  • @WernerHenze Can I call that a default initialization? Anyway, that would allow to instantiate an object with only that default value, but what I wanted is to be able to instantiate it to any value, as if having a constructor with a parameter but using only the default compiler-generated constructor. Looks like is not possible, what I would like a confirmation – some_math_guy Feb 28 '21 at 17:06
  • 1
    Since `a` is private and `privateSeta` is private, then you can't access either of those things from main*, no. *There is a workaround but it's stupid and dangerous and you shouldn't do it. – Mooing Duck Feb 28 '21 at 17:07
  • `class B { int a = 10; /* the rest without changes */ };` Is this what you are looking for? With this, `B b2;` would have `b2.a == 10` – Igor Tandetnik Feb 28 '21 at 17:09
  • @Mooing Duck So using publicSeta is not in the spirit of keeping classes encapsulated either, then the only proper way to do it is with a custom constructor?. And what is this workaorund, just for fun? – some_math_guy Feb 28 '21 at 17:10
  • 1
    Yes, if you want to construct a class in a custom way, the way to do that is with a custom constructor. http://coliru.stacked-crooked.com/a/b7a797c40b9f25cb – Mooing Duck Feb 28 '21 at 17:13
  • @Igor Tandetnik, that limits it to that specific value, if I want to create another object with another value I wouldn't be able. If I had a custom constructor that initializes a, I would do B(10) or B(17) or whatever, without changing the code everytime. I was wondering if somehow I can do that with the default empty constructor – some_math_guy Feb 28 '21 at 17:14
  • Are you saying you want the same line, `B b;` to set `b.a` to some value sometimes, and to a different value other times? By what magic would the constructor know what value you meant to set the member to in this particular spot? Perhaps I misunderstand the question. Could you show an example of expected usage, and the expected outcome of that example? In other words, could you show a [mcve]? – Igor Tandetnik Feb 28 '21 at 17:19
  • @Mooing Duck , Nice one, I think I can conclude that exceot for this very dangerous think, the answer to question 2 is that it is not possible – some_math_guy Feb 28 '21 at 17:21
  • @IgorTandetnik Not by magic, but I was wondering if there was some way/ some syntax to pass values to a default compiler-generated constructor. I wanted to confirm the only way is to write a custom constructor that passes the value – some_math_guy Feb 28 '21 at 17:25
  • The syntax to pass values to a constructor, or indeed to any function, is a parameter. What's wrong with writing a constructor taking a parameter? That's a rather obvious solution, I wonder what makes you look away from it. Your question sounds like an [XY problem](https://en.wikipedia.org/wiki/XY_problem), like there's a real, as yet unstated, problem lurking in the background, for which you are hoping that "a magical way to pass parameters to a function taking no parameters" would be a solution. – Igor Tandetnik Feb 28 '21 at 17:32

0 Answers0